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 { |
Chris Lattner | 541d890 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 61 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 62 | O << *static_cast<const X86TargetLowering*>(TLI)->getPICBaseSymbol(MF, |
| 63 | OutContext); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 66 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 67 | /// method to print assembly for each instruction. |
| 68 | /// |
| 69 | bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 70 | SetupMachineFunction(MF); |
| 71 | O << "\n\n"; |
Chris Lattner | e598067 | 2010-01-26 23:18:44 +0000 | [diff] [blame^] | 72 | |
| 73 | // COFF and Cygwin specific mangling stuff. This should be moved out to the |
| 74 | // mangler or handled some other way? |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 75 | if (Subtarget->isTargetCOFF()) { |
| 76 | X86COFFMachineModuleInfo &COFFMMI = |
Chris Lattner | e598067 | 2010-01-26 23:18:44 +0000 | [diff] [blame^] | 77 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 78 | |
| 79 | // Populate function information map. Don't want to populate |
| 80 | // non-stdcall or non-fastcall functions' information right now. |
Chris Lattner | e598067 | 2010-01-26 23:18:44 +0000 | [diff] [blame^] | 81 | const Function *F = MF.getFunction(); |
| 82 | CallingConv::ID CC = F->getCallingConv(); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 83 | if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) |
| 84 | COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>()); |
| 85 | } |
Chris Lattner | e598067 | 2010-01-26 23:18:44 +0000 | [diff] [blame^] | 86 | if (Subtarget->isTargetCygMing()) { |
| 87 | const Function *F = MF.getFunction(); |
| 88 | X86COFFMachineModuleInfo &COFFMMI = |
| 89 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
| 90 | COFFMMI.DecorateCygMingName(CurrentFnSym, OutContext,F,*TM.getTargetData()); |
| 91 | |
| 92 | O << "\t.def\t " << *CurrentFnSym; |
| 93 | O << ";\t.scl\t" << |
| 94 | (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT) |
| 95 | << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) |
| 96 | << ";\t.endef\n"; |
| 97 | } |
| 98 | |
| 99 | // Have common code print out the function header with linkage info etc. |
| 100 | EmitFunctionHeader(); |
| 101 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 102 | |
| 103 | // Print out code for the function. |
| 104 | bool hasAnyRealCode = false; |
| 105 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 106 | I != E; ++I) { |
| 107 | // Print a label for the basic block. |
Dan Gohman | 5fda434 | 2009-10-06 17:38:38 +0000 | [diff] [blame] | 108 | EmitBasicBlockStart(I); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 109 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 110 | II != IE; ++II) { |
| 111 | // Print the assembly for the instruction. |
| 112 | if (!II->isLabel()) |
| 113 | hasAnyRealCode = true; |
| 114 | printMachineInstruction(II); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (Subtarget->isTargetDarwin() && !hasAnyRealCode) { |
| 119 | // If the function is empty, then we need to emit *something*. Otherwise, |
| 120 | // the function's label might be associated with something that it wasn't |
| 121 | // meant to be associated with. We emit a noop in this situation. |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 122 | O << "\tnop\n"; |
| 123 | } |
| 124 | |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 125 | if (MAI->hasDotTypeDotSizeDirective()) |
| 126 | O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 127 | |
| 128 | // Emit post-function debug information. |
| 129 | if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling()) |
| 130 | DW->EndFunction(&MF); |
| 131 | |
| 132 | // Print out jump tables referenced by the function. |
Chris Lattner | 1f8c892 | 2010-01-25 22:41:33 +0000 | [diff] [blame] | 133 | EmitJumpTableInfo(MF); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 134 | |
| 135 | // We didn't modify anything. |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /// printSymbolOperand - Print a raw symbol reference operand. This handles |
| 140 | /// jump tables, constant pools, global address and external symbols, all of |
| 141 | /// which print to a label with various suffixes for relocation types etc. |
| 142 | void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) { |
| 143 | switch (MO.getType()) { |
| 144 | default: llvm_unreachable("unknown symbol type!"); |
| 145 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 242edfc | 2010-01-23 05:26:25 +0000 | [diff] [blame] | 146 | O << *GetJTISymbol(MO.getIndex()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 147 | break; |
| 148 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 242edfc | 2010-01-23 05:26:25 +0000 | [diff] [blame] | 149 | O << *GetCPISymbol(MO.getIndex()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 150 | printOffset(MO.getOffset()); |
| 151 | break; |
| 152 | case MachineOperand::MO_GlobalAddress: { |
| 153 | const GlobalValue *GV = MO.getGlobal(); |
| 154 | |
Chris Lattner | b6ed586 | 2010-01-18 00:59:24 +0000 | [diff] [blame] | 155 | MCSymbol *GVSym; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 156 | if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 157 | GVSym = GetSymbolWithGlobalValueBase(GV, "$stub"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 158 | else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || |
| 159 | MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE || |
| 160 | MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE) |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 161 | GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 162 | else |
| 163 | GVSym = GetGlobalValueSymbol(GV); |
| 164 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 165 | if (Subtarget->isTargetCygMing()) { |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 166 | X86COFFMachineModuleInfo &COFFMMI = |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 167 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 168 | COFFMMI.DecorateCygMingName(GVSym, OutContext, GV, *TM.getTargetData()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // Handle dllimport linkage. |
| 172 | if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 173 | GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 174 | |
| 175 | if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY || |
| 176 | MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) { |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 177 | MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 178 | |
| 179 | const MCSymbol *&StubSym = |
| 180 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 181 | if (StubSym == 0) |
| 182 | StubSym = GetGlobalValueSymbol(GV); |
| 183 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 184 | } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){ |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 185 | MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 186 | const MCSymbol *&StubSym = |
| 187 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 188 | if (StubSym == 0) |
| 189 | StubSym = GetGlobalValueSymbol(GV); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 190 | } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) { |
Chris Lattner | a6f2a10 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 191 | MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub"); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 192 | const MCSymbol *&StubSym = |
| 193 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym); |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 194 | if (StubSym == 0) |
| 195 | StubSym = GetGlobalValueSymbol(GV); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // If the name begins with a dollar-sign, enclose it in parens. We do this |
| 199 | // to avoid having it look like an integer immediate to the assembler. |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 200 | if (GVSym->getName()[0] != '$') |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 201 | O << *GVSym; |
| 202 | else |
| 203 | O << '(' << *GVSym << ')'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 204 | printOffset(MO.getOffset()); |
| 205 | break; |
| 206 | } |
| 207 | case MachineOperand::MO_ExternalSymbol: { |
Chris Lattner | f80743e | 2010-01-13 07:56:59 +0000 | [diff] [blame] | 208 | const MCSymbol *SymToPrint; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 209 | if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 210 | SmallString<128> TempNameStr; |
| 211 | TempNameStr += StringRef(MO.getSymbolName()); |
| 212 | TempNameStr += StringRef("$stub"); |
| 213 | |
| 214 | const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 215 | const MCSymbol *&StubSym = |
| 216 | MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym); |
| 217 | if (StubSym == 0) { |
Chris Lattner | a38b287 | 2010-01-13 06:38:18 +0000 | [diff] [blame] | 218 | TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end()); |
| 219 | StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | f80743e | 2010-01-13 07:56:59 +0000 | [diff] [blame] | 221 | SymToPrint = StubSym; |
| 222 | } else { |
Chris Lattner | 178a932 | 2010-01-16 01:00:27 +0000 | [diff] [blame] | 223 | SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName()); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // If the name begins with a dollar-sign, enclose it in parens. We do this |
| 227 | // to avoid having it look like an integer immediate to the assembler. |
Chris Lattner | f80743e | 2010-01-13 07:56:59 +0000 | [diff] [blame] | 228 | if (SymToPrint->getName()[0] != '$') |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 229 | O << *SymToPrint; |
| 230 | else |
| 231 | O << '(' << *SymToPrint << '('; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | switch (MO.getTargetFlags()) { |
| 237 | default: |
| 238 | llvm_unreachable("Unknown target flag on GV operand"); |
| 239 | case X86II::MO_NO_FLAG: // No flag. |
| 240 | break; |
| 241 | case X86II::MO_DARWIN_NONLAZY: |
| 242 | case X86II::MO_DLLIMPORT: |
| 243 | case X86II::MO_DARWIN_STUB: |
| 244 | // These affect the name of the symbol, not any suffix. |
| 245 | break; |
| 246 | case X86II::MO_GOT_ABSOLUTE_ADDRESS: |
| 247 | O << " + [.-"; |
| 248 | PrintPICBaseSymbol(); |
| 249 | O << ']'; |
| 250 | break; |
| 251 | case X86II::MO_PIC_BASE_OFFSET: |
| 252 | case X86II::MO_DARWIN_NONLAZY_PIC_BASE: |
| 253 | case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: |
| 254 | O << '-'; |
| 255 | PrintPICBaseSymbol(); |
| 256 | break; |
| 257 | case X86II::MO_TLSGD: O << "@TLSGD"; break; |
| 258 | case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break; |
| 259 | case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break; |
| 260 | case X86II::MO_TPOFF: O << "@TPOFF"; break; |
| 261 | case X86II::MO_NTPOFF: O << "@NTPOFF"; break; |
| 262 | case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break; |
| 263 | case X86II::MO_GOT: O << "@GOT"; break; |
| 264 | case X86II::MO_GOTOFF: O << "@GOTOFF"; break; |
| 265 | case X86II::MO_PLT: O << "@PLT"; break; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /// print_pcrel_imm - This is used to print an immediate value that ends up |
| 270 | /// being encoded as a pc-relative value. These print slightly differently, for |
| 271 | /// example, a $ is not emitted. |
| 272 | void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) { |
| 273 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 274 | switch (MO.getType()) { |
| 275 | default: llvm_unreachable("Unknown pcrel immediate operand"); |
| 276 | case MachineOperand::MO_Immediate: |
| 277 | O << MO.getImm(); |
| 278 | return; |
| 279 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 84d5ca9 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 280 | O << *MO.getMBB()->getSymbol(OutContext); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 281 | return; |
| 282 | case MachineOperand::MO_GlobalAddress: |
| 283 | case MachineOperand::MO_ExternalSymbol: |
| 284 | printSymbolOperand(MO); |
| 285 | return; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
| 290 | void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | b6ed586 | 2010-01-18 00:59:24 +0000 | [diff] [blame] | 291 | const char *Modifier) { |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 292 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 293 | switch (MO.getType()) { |
| 294 | default: llvm_unreachable("unknown operand type!"); |
| 295 | case MachineOperand::MO_Register: { |
| 296 | O << '%'; |
| 297 | unsigned Reg = MO.getReg(); |
| 298 | if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { |
| 299 | EVT VT = (strcmp(Modifier+6,"64") == 0) ? |
| 300 | MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 : |
| 301 | ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8)); |
| 302 | Reg = getX86SubSuperRegister(Reg, VT); |
| 303 | } |
| 304 | O << X86ATTInstPrinter::getRegisterName(Reg); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | case MachineOperand::MO_Immediate: |
| 309 | O << '$' << MO.getImm(); |
| 310 | return; |
| 311 | |
| 312 | case MachineOperand::MO_JumpTableIndex: |
| 313 | case MachineOperand::MO_ConstantPoolIndex: |
| 314 | case MachineOperand::MO_GlobalAddress: |
| 315 | case MachineOperand::MO_ExternalSymbol: { |
| 316 | O << '$'; |
| 317 | printSymbolOperand(MO); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) { |
| 324 | unsigned char value = MI->getOperand(Op).getImm(); |
| 325 | assert(value <= 7 && "Invalid ssecc argument!"); |
| 326 | switch (value) { |
| 327 | case 0: O << "eq"; break; |
| 328 | case 1: O << "lt"; break; |
| 329 | case 2: O << "le"; break; |
| 330 | case 3: O << "unord"; break; |
| 331 | case 4: O << "neq"; break; |
| 332 | case 5: O << "nlt"; break; |
| 333 | case 6: O << "nle"; break; |
| 334 | case 7: O << "ord"; break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op, |
| 339 | const char *Modifier) { |
| 340 | const MachineOperand &BaseReg = MI->getOperand(Op); |
| 341 | const MachineOperand &IndexReg = MI->getOperand(Op+2); |
| 342 | const MachineOperand &DispSpec = MI->getOperand(Op+3); |
| 343 | |
| 344 | // If we really don't want to print out (rip), don't. |
| 345 | bool HasBaseReg = BaseReg.getReg() != 0; |
| 346 | if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") && |
| 347 | BaseReg.getReg() == X86::RIP) |
| 348 | HasBaseReg = false; |
| 349 | |
| 350 | // HasParenPart - True if we will print out the () part of the mem ref. |
| 351 | bool HasParenPart = IndexReg.getReg() || HasBaseReg; |
| 352 | |
| 353 | if (DispSpec.isImm()) { |
| 354 | int DispVal = DispSpec.getImm(); |
| 355 | if (DispVal || !HasParenPart) |
| 356 | O << DispVal; |
| 357 | } else { |
| 358 | assert(DispSpec.isGlobal() || DispSpec.isCPI() || |
| 359 | DispSpec.isJTI() || DispSpec.isSymbol()); |
| 360 | printSymbolOperand(MI->getOperand(Op+3)); |
| 361 | } |
| 362 | |
| 363 | if (HasParenPart) { |
| 364 | assert(IndexReg.getReg() != X86::ESP && |
| 365 | "X86 doesn't allow scaling by ESP"); |
| 366 | |
| 367 | O << '('; |
| 368 | if (HasBaseReg) |
| 369 | printOperand(MI, Op, Modifier); |
| 370 | |
| 371 | if (IndexReg.getReg()) { |
| 372 | O << ','; |
| 373 | printOperand(MI, Op+2, Modifier); |
| 374 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
| 375 | if (ScaleVal != 1) |
| 376 | O << ',' << ScaleVal; |
| 377 | } |
| 378 | O << ')'; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op, |
| 383 | const char *Modifier) { |
| 384 | assert(isMem(MI, Op) && "Invalid memory reference!"); |
| 385 | const MachineOperand &Segment = MI->getOperand(Op+4); |
| 386 | if (Segment.getReg()) { |
| 387 | printOperand(MI, Op+4, Modifier); |
| 388 | O << ':'; |
| 389 | } |
| 390 | printLeaMemReference(MI, Op, Modifier); |
| 391 | } |
| 392 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 393 | void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) { |
| 394 | PrintPICBaseSymbol(); |
| 395 | O << '\n'; |
| 396 | PrintPICBaseSymbol(); |
| 397 | O << ':'; |
| 398 | } |
| 399 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 400 | bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) { |
| 401 | unsigned Reg = MO.getReg(); |
| 402 | switch (Mode) { |
| 403 | default: return true; // Unknown mode. |
| 404 | case 'b': // Print QImode register |
| 405 | Reg = getX86SubSuperRegister(Reg, MVT::i8); |
| 406 | break; |
| 407 | case 'h': // Print QImode high register |
| 408 | Reg = getX86SubSuperRegister(Reg, MVT::i8, true); |
| 409 | break; |
| 410 | case 'w': // Print HImode register |
| 411 | Reg = getX86SubSuperRegister(Reg, MVT::i16); |
| 412 | break; |
| 413 | case 'k': // Print SImode register |
| 414 | Reg = getX86SubSuperRegister(Reg, MVT::i32); |
| 415 | break; |
| 416 | case 'q': // Print DImode register |
| 417 | Reg = getX86SubSuperRegister(Reg, MVT::i64); |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | O << '%' << X86ATTInstPrinter::getRegisterName(Reg); |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 426 | /// |
| 427 | bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 428 | unsigned AsmVariant, |
| 429 | const char *ExtraCode) { |
| 430 | // Does this asm operand have a single letter operand modifier? |
| 431 | if (ExtraCode && ExtraCode[0]) { |
| 432 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 433 | |
| 434 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 435 | |
| 436 | switch (ExtraCode[0]) { |
| 437 | default: return true; // Unknown modifier. |
| 438 | case 'a': // This is an address. Currently only 'i' and 'r' are expected. |
| 439 | if (MO.isImm()) { |
| 440 | O << MO.getImm(); |
| 441 | return false; |
| 442 | } |
| 443 | if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) { |
| 444 | printSymbolOperand(MO); |
| 445 | return false; |
| 446 | } |
| 447 | if (MO.isReg()) { |
| 448 | O << '('; |
| 449 | printOperand(MI, OpNo); |
| 450 | O << ')'; |
| 451 | return false; |
| 452 | } |
| 453 | return true; |
| 454 | |
| 455 | case 'c': // Don't print "$" before a global var name or constant. |
| 456 | if (MO.isImm()) |
| 457 | O << MO.getImm(); |
| 458 | else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) |
| 459 | printSymbolOperand(MO); |
| 460 | else |
| 461 | printOperand(MI, OpNo); |
| 462 | return false; |
| 463 | |
| 464 | case 'A': // Print '*' before a register (it must be a register) |
| 465 | if (MO.isReg()) { |
| 466 | O << '*'; |
| 467 | printOperand(MI, OpNo); |
| 468 | return false; |
| 469 | } |
| 470 | return true; |
| 471 | |
| 472 | case 'b': // Print QImode register |
| 473 | case 'h': // Print QImode high register |
| 474 | case 'w': // Print HImode register |
| 475 | case 'k': // Print SImode register |
| 476 | case 'q': // Print DImode register |
| 477 | if (MO.isReg()) |
| 478 | return printAsmMRegister(MO, ExtraCode[0]); |
| 479 | printOperand(MI, OpNo); |
| 480 | return false; |
| 481 | |
| 482 | case 'P': // This is the operand of a call, treat specially. |
| 483 | print_pcrel_imm(MI, OpNo); |
| 484 | return false; |
| 485 | |
| 486 | case 'n': // Negate the immediate or print a '-' before the operand. |
| 487 | // Note: this is a temporary solution. It should be handled target |
| 488 | // independently as part of the 'MC' work. |
| 489 | if (MO.isImm()) { |
| 490 | O << -MO.getImm(); |
| 491 | return false; |
| 492 | } |
| 493 | O << '-'; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | printOperand(MI, OpNo); |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 502 | unsigned OpNo, unsigned AsmVariant, |
| 503 | const char *ExtraCode) { |
| 504 | if (ExtraCode && ExtraCode[0]) { |
| 505 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 506 | |
| 507 | switch (ExtraCode[0]) { |
| 508 | default: return true; // Unknown modifier. |
| 509 | case 'b': // Print QImode register |
| 510 | case 'h': // Print QImode high register |
| 511 | case 'w': // Print HImode register |
| 512 | case 'k': // Print SImode register |
| 513 | case 'q': // Print SImode register |
| 514 | // These only apply to registers, ignore on mem. |
| 515 | break; |
| 516 | case 'P': // Don't print @PLT, but do print as memory. |
| 517 | printMemReference(MI, OpNo, "no-rip"); |
| 518 | return false; |
| 519 | } |
| 520 | } |
| 521 | printMemReference(MI, OpNo); |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | |
| 527 | /// printMachineInstruction -- Print out a single X86 LLVM instruction MI in |
| 528 | /// AT&T syntax to the current output stream. |
| 529 | /// |
| 530 | void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 531 | ++EmittedInsts; |
| 532 | |
Devang Patel | 5450fc1 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 533 | processDebugLoc(MI, true); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 534 | |
| 535 | printInstructionThroughMCStreamer(MI); |
| 536 | |
David Greene | ca9b04b | 2009-11-13 21:34:57 +0000 | [diff] [blame] | 537 | if (VerboseAsm) |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 538 | EmitComments(*MI); |
| 539 | O << '\n'; |
Devang Patel | 5450fc1 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 540 | |
| 541 | processDebugLoc(MI, false); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 544 | void X86AsmPrinter::EmitEndOfAsmFile(Module &M) { |
| 545 | if (Subtarget->isTargetDarwin()) { |
| 546 | // All darwin targets use mach-o. |
| 547 | TargetLoweringObjectFileMachO &TLOFMacho = |
| 548 | static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering()); |
| 549 | |
| 550 | MachineModuleInfoMachO &MMIMacho = |
| 551 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 552 | |
| 553 | // Output stubs for dynamically-linked functions. |
| 554 | MachineModuleInfoMachO::SymbolListTy Stubs; |
| 555 | |
| 556 | Stubs = MMIMacho.GetFnStubList(); |
| 557 | if (!Stubs.empty()) { |
| 558 | const MCSection *TheSection = |
| 559 | TLOFMacho.getMachOSection("__IMPORT", "__jump_table", |
| 560 | MCSectionMachO::S_SYMBOL_STUBS | |
| 561 | MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE | |
| 562 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 563 | 5, SectionKind::getMetadata()); |
| 564 | OutStreamer.SwitchSection(TheSection); |
| 565 | |
| 566 | for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 567 | O << *Stubs[i].first << ":\n"; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 568 | // Get the MCSymbol without the $stub suffix. |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 569 | O << "\t.indirect_symbol " << *Stubs[i].second; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 570 | O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n"; |
| 571 | } |
| 572 | O << '\n'; |
| 573 | |
| 574 | Stubs.clear(); |
| 575 | } |
| 576 | |
| 577 | // Output stubs for external and common global variables. |
| 578 | Stubs = MMIMacho.GetGVStubList(); |
| 579 | if (!Stubs.empty()) { |
| 580 | const MCSection *TheSection = |
| 581 | TLOFMacho.getMachOSection("__IMPORT", "__pointers", |
| 582 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, |
| 583 | SectionKind::getMetadata()); |
| 584 | OutStreamer.SwitchSection(TheSection); |
| 585 | |
| 586 | for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 587 | O << *Stubs[i].first << ":\n\t.indirect_symbol " << *Stubs[i].second; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 588 | O << "\n\t.long\t0\n"; |
| 589 | } |
| 590 | Stubs.clear(); |
| 591 | } |
| 592 | |
| 593 | Stubs = MMIMacho.GetHiddenGVStubList(); |
| 594 | if (!Stubs.empty()) { |
| 595 | OutStreamer.SwitchSection(getObjFileLowering().getDataSection()); |
| 596 | EmitAlignment(2); |
| 597 | |
| 598 | for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 599 | O << *Stubs[i].first << ":\n" << MAI->getData32bitsDirective(); |
| 600 | O << *Stubs[i].second << '\n'; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 601 | } |
| 602 | Stubs.clear(); |
| 603 | } |
| 604 | |
| 605 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 606 | // contain code that falls through to other global symbols (e.g. the obvious |
| 607 | // implementation of multiple entry points). If this doesn't occur, the |
| 608 | // linker can safely perform dead code stripping. Since LLVM never |
| 609 | // generates code that does this, it is always safe to set. |
Chris Lattner | 2d7c814 | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 610 | OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 613 | if (Subtarget->isTargetCOFF()) { |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 614 | X86COFFMachineModuleInfo &COFFMMI = |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 615 | MMI->getObjFileInfo<X86COFFMachineModuleInfo>(); |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 616 | |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 617 | // Emit type information for external functions |
| 618 | for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(), |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 619 | E = COFFMMI.stub_end(); I != E; ++I) { |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 620 | O << "\t.def\t " << I->getKeyData() |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 621 | << ";\t.scl\t" << COFF::C_EXT |
| 622 | << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) |
| 623 | << ";\t.endef\n"; |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 624 | } |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 625 | |
| 626 | if (Subtarget->isTargetCygMing()) { |
| 627 | // Necessary for dllexport support |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 628 | std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals; |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 629 | |
| 630 | TargetLoweringObjectFileCOFF &TLOFCOFF = |
| 631 | static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering()); |
| 632 | |
| 633 | for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 634 | if (I->hasDLLExportLinkage()) { |
Chris Lattner | b6ed586 | 2010-01-18 00:59:24 +0000 | [diff] [blame] | 635 | MCSymbol *Sym = GetGlobalValueSymbol(I); |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 636 | COFFMMI.DecorateCygMingName(Sym, OutContext, I, *TM.getTargetData()); |
| 637 | DLLExportedFns.push_back(Sym); |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | for (Module::const_global_iterator I = M.global_begin(), |
| 641 | E = M.global_end(); I != E; ++I) |
Chris Lattner | c53c24c | 2010-01-16 00:51:39 +0000 | [diff] [blame] | 642 | if (I->hasDLLExportLinkage()) |
| 643 | DLLExportedGlobals.push_back(GetGlobalValueSymbol(I)); |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 644 | |
| 645 | // Output linker support code for dllexported globals on windows. |
| 646 | if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) { |
| 647 | OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve", |
| 648 | true, |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 649 | SectionKind::getMetadata())); |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 650 | for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i) |
| 651 | O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n"; |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 652 | |
Chris Lattner | ce40984 | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 653 | for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i) |
| 654 | O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n"; |
Anton Korobeynikov | d5fcb1d | 2009-10-15 22:36:18 +0000 | [diff] [blame] | 655 | } |
Chris Lattner | bf4b6a8 | 2009-09-20 07:41:30 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | |
| 661 | //===----------------------------------------------------------------------===// |
| 662 | // Target Registry Stuff |
| 663 | //===----------------------------------------------------------------------===// |
| 664 | |
| 665 | static MCInstPrinter *createX86MCInstPrinter(const Target &T, |
| 666 | unsigned SyntaxVariant, |
| 667 | const MCAsmInfo &MAI, |
| 668 | raw_ostream &O) { |
| 669 | if (SyntaxVariant == 0) |
| 670 | return new X86ATTInstPrinter(O, MAI); |
| 671 | if (SyntaxVariant == 1) |
| 672 | return new X86IntelInstPrinter(O, MAI); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | // Force static initialization. |
| 677 | extern "C" void LLVMInitializeX86AsmPrinter() { |
| 678 | RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target); |
| 679 | RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target); |
| 680 | |
| 681 | TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter); |
| 682 | TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter); |
| 683 | } |