Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===// |
| 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 | // |
| 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'. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "asm-printer" |
| 17 | #include "X86ATTAsmPrinter.h" |
| 18 | #include "X86.h" |
| 19 | #include "X86COFF.h" |
| 20 | #include "X86MachineFunctionInfo.h" |
| 21 | #include "X86TargetMachine.h" |
| 22 | #include "X86TargetAsmInfo.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/CallingConv.h" |
Anton Korobeynikov | 5772c67 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/Support/Mangler.h" |
| 28 | #include "llvm/Target/TargetAsmInfo.h" |
| 29 | #include "llvm/Target/TargetOptions.h" |
| 30 | #include "llvm/ADT/Statistic.h" |
| 31 | using namespace llvm; |
| 32 | |
| 33 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 34 | |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 35 | static std::string getPICLabelString(unsigned FnNum, |
| 36 | const TargetAsmInfo *TAI, |
| 37 | const X86Subtarget* Subtarget) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | std::string label; |
| 39 | if (Subtarget->isTargetDarwin()) |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 40 | label = "\"L" + utostr_32(FnNum) + "$pb\""; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | else if (Subtarget->isTargetELF()) |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 42 | label = ".Lllvm$" + utostr_32(FnNum) + "." + "$piclabel"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | else |
| 44 | assert(0 && "Don't know how to print PIC label!\n"); |
| 45 | |
| 46 | return label; |
| 47 | } |
| 48 | |
| 49 | /// getSectionForFunction - Return the section that we should emit the |
| 50 | /// specified function body into. |
| 51 | std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const { |
| 52 | switch (F.getLinkage()) { |
| 53 | default: assert(0 && "Unknown linkage type!"); |
| 54 | case Function::InternalLinkage: |
| 55 | case Function::DLLExportLinkage: |
| 56 | case Function::ExternalLinkage: |
| 57 | return TAI->getTextSection(); |
| 58 | case Function::WeakLinkage: |
| 59 | case Function::LinkOnceLinkage: |
| 60 | if (Subtarget->isTargetDarwin()) { |
| 61 | return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions"; |
| 62 | } else if (Subtarget->isTargetCygMing()) { |
| 63 | return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\""; |
| 64 | } else { |
| 65 | return "\t.section\t.llvm.linkonce.t." + CurrentFnName + |
| 66 | ",\"ax\",@progbits"; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 72 | /// method to print assembly for each instruction. |
| 73 | /// |
| 74 | bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 75 | if (TAI->doesSupportDebugInformation()) { |
| 76 | // Let PassManager know we need debug information and relay |
| 77 | // the MachineModuleInfo address on to DwarfWriter. |
Bill Wendling | d1bda4f | 2007-09-11 08:27:17 +0000 | [diff] [blame] | 78 | MMI = &getAnalysis<MachineModuleInfo>(); |
| 79 | DW.SetModuleInfo(MMI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | SetupMachineFunction(MF); |
| 83 | O << "\n\n"; |
| 84 | |
| 85 | // Print out constants referenced by the function |
| 86 | EmitConstantPool(MF.getConstantPool()); |
| 87 | |
| 88 | // Print out labels for the function. |
| 89 | const Function *F = MF.getFunction(); |
| 90 | unsigned CC = F->getCallingConv(); |
| 91 | |
| 92 | // Populate function information map. Actually, We don't want to populate |
| 93 | // non-stdcall or non-fastcall functions' information right now. |
| 94 | if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) |
| 95 | FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>(); |
| 96 | |
| 97 | X86SharedAsmPrinter::decorateName(CurrentFnName, F); |
| 98 | |
| 99 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
| 100 | |
Evan Cheng | 2e8d3d4 | 2008-03-25 22:29:46 +0000 | [diff] [blame] | 101 | unsigned FnAlign = OptimizeForSize ? 1 : 4; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 102 | switch (F->getLinkage()) { |
| 103 | default: assert(0 && "Unknown linkage type!"); |
| 104 | case Function::InternalLinkage: // Symbols default to internal. |
Evan Cheng | 2e8d3d4 | 2008-03-25 22:29:46 +0000 | [diff] [blame] | 105 | EmitAlignment(FnAlign, F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | break; |
| 107 | case Function::DLLExportLinkage: |
| 108 | DLLExportedFns.insert(Mang->makeNameProper(F->getName(), "")); |
| 109 | //FALLS THROUGH |
| 110 | case Function::ExternalLinkage: |
Evan Cheng | 2e8d3d4 | 2008-03-25 22:29:46 +0000 | [diff] [blame] | 111 | EmitAlignment(FnAlign, F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 113 | break; |
| 114 | case Function::LinkOnceLinkage: |
| 115 | case Function::WeakLinkage: |
Evan Cheng | 2e8d3d4 | 2008-03-25 22:29:46 +0000 | [diff] [blame] | 116 | EmitAlignment(FnAlign, F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | if (Subtarget->isTargetDarwin()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 119 | O << TAI->getWeakDefDirective() << CurrentFnName << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | } else if (Subtarget->isTargetCygMing()) { |
Dan Gohman | c37918d | 2007-10-05 15:54:58 +0000 | [diff] [blame] | 121 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | O << "\t.linkonce discard\n"; |
| 123 | } else { |
Dan Gohman | 721e658 | 2007-07-30 15:08:02 +0000 | [diff] [blame] | 124 | O << "\t.weak\t" << CurrentFnName << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 125 | } |
| 126 | break; |
| 127 | } |
| 128 | if (F->hasHiddenVisibility()) { |
| 129 | if (const char *Directive = TAI->getHiddenDirective()) |
| 130 | O << Directive << CurrentFnName << "\n"; |
| 131 | } else if (F->hasProtectedVisibility()) { |
| 132 | if (const char *Directive = TAI->getProtectedDirective()) |
| 133 | O << Directive << CurrentFnName << "\n"; |
| 134 | } |
| 135 | |
| 136 | if (Subtarget->isTargetELF()) |
Dan Gohman | 721e658 | 2007-07-30 15:08:02 +0000 | [diff] [blame] | 137 | O << "\t.type\t" << CurrentFnName << ",@function\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 138 | else if (Subtarget->isTargetCygMing()) { |
| 139 | O << "\t.def\t " << CurrentFnName |
| 140 | << ";\t.scl\t" << |
| 141 | (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT) |
| 142 | << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) |
| 143 | << ";\t.endef\n"; |
| 144 | } |
| 145 | |
| 146 | O << CurrentFnName << ":\n"; |
| 147 | // Add some workaround for linkonce linkage on Cygwin\MinGW |
| 148 | if (Subtarget->isTargetCygMing() && |
| 149 | (F->getLinkage() == Function::LinkOnceLinkage || |
| 150 | F->getLinkage() == Function::WeakLinkage)) |
| 151 | O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n"; |
| 152 | |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame^] | 153 | if (TAI->doesSupportDebugInformation() || |
| 154 | TAI->doesSupportExceptionHandling()) { |
| 155 | // Emit pre-function debug and/or EH information. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 156 | DW.BeginFunction(&MF); |
| 157 | } |
| 158 | |
Bill Wendling | b5880a7 | 2008-01-26 09:03:52 +0000 | [diff] [blame] | 159 | if (Subtarget->isTargetDarwin()) { |
| 160 | // If the function is empty, then we need to emit *something*. Otherwise, |
| 161 | // the function's label might be associated with something that it wasn't |
| 162 | // meant to be associated with. We emit a noop in this situation. |
| 163 | MachineFunction::iterator I = MF.begin(); |
| 164 | |
| 165 | if (++I == MF.end() && MF.front().empty()) |
| 166 | O << "\tnop\n"; |
| 167 | } |
| 168 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | // Print out code for the function. |
| 170 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 171 | I != E; ++I) { |
| 172 | // Print a label for the basic block. |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 173 | if (!I->pred_empty()) { |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 174 | printBasicBlockLabel(I, true, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | O << '\n'; |
| 176 | } |
Bill Wendling | b5880a7 | 2008-01-26 09:03:52 +0000 | [diff] [blame] | 177 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 178 | II != IE; ++II) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 179 | // Print the assembly for the instruction. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 180 | printMachineInstruction(II); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (TAI->hasDotTypeDotSizeDirective()) |
Dan Gohman | dd1aa3a | 2007-08-01 14:42:30 +0000 | [diff] [blame] | 185 | O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 186 | |
| 187 | if (TAI->doesSupportDebugInformation()) { |
| 188 | // Emit post-function debug information. |
| 189 | DW.EndFunction(); |
| 190 | } |
| 191 | |
| 192 | // Print out jump tables referenced by the function. |
| 193 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
| 194 | |
| 195 | // We didn't modify anything. |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | static inline bool printGOT(TargetMachine &TM, const X86Subtarget* ST) { |
| 200 | return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_; |
| 201 | } |
| 202 | |
| 203 | static inline bool printStub(TargetMachine &TM, const X86Subtarget* ST) { |
| 204 | return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static; |
| 205 | } |
| 206 | |
| 207 | void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, |
| 208 | const char *Modifier, bool NotRIPRel) { |
| 209 | const MachineOperand &MO = MI->getOperand(OpNo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | switch (MO.getType()) { |
| 211 | case MachineOperand::MO_Register: { |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 212 | assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 213 | "Virtual registers should not make it this far!"); |
| 214 | O << '%'; |
| 215 | unsigned Reg = MO.getReg(); |
| 216 | if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { |
| 217 | MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ? |
| 218 | MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 : |
| 219 | ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8)); |
| 220 | Reg = getX86SubSuperRegister(Reg, VT); |
| 221 | } |
Evan Cheng | 3c0eda5 | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 222 | for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 223 | O << (char)tolower(*Name); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | case MachineOperand::MO_Immediate: |
| 228 | if (!Modifier || |
| 229 | (strcmp(Modifier, "debug") && strcmp(Modifier, "mem"))) |
| 230 | O << '$'; |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 231 | O << MO.getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | return; |
| 233 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 234 | printBasicBlockLabel(MO.getMBB()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 235 | return; |
| 236 | case MachineOperand::MO_JumpTableIndex: { |
| 237 | bool isMemOp = Modifier && !strcmp(Modifier, "mem"); |
| 238 | if (!isMemOp) O << '$'; |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 239 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_" |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 240 | << MO.getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 241 | |
| 242 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| 243 | if (Subtarget->isPICStyleStub()) |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 244 | O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber() |
| 245 | << "$pb\""; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | else if (Subtarget->isPICStyleGOT()) |
| 247 | O << "@GOTOFF"; |
| 248 | } |
| 249 | |
| 250 | if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel) |
| 251 | O << "(%rip)"; |
| 252 | return; |
| 253 | } |
| 254 | case MachineOperand::MO_ConstantPoolIndex: { |
| 255 | bool isMemOp = Modifier && !strcmp(Modifier, "mem"); |
| 256 | if (!isMemOp) O << '$'; |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 257 | O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_" |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 258 | << MO.getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 259 | |
| 260 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| 261 | if (Subtarget->isPICStyleStub()) |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 262 | O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber() |
| 263 | << "$pb\""; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 264 | else if (Subtarget->isPICStyleGOT()) |
| 265 | O << "@GOTOFF"; |
| 266 | } |
| 267 | |
| 268 | int Offset = MO.getOffset(); |
| 269 | if (Offset > 0) |
| 270 | O << "+" << Offset; |
| 271 | else if (Offset < 0) |
| 272 | O << Offset; |
| 273 | |
| 274 | if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel) |
| 275 | O << "(%rip)"; |
| 276 | return; |
| 277 | } |
| 278 | case MachineOperand::MO_GlobalAddress: { |
| 279 | bool isCallOp = Modifier && !strcmp(Modifier, "call"); |
| 280 | bool isMemOp = Modifier && !strcmp(Modifier, "mem"); |
| 281 | bool needCloseParen = false; |
| 282 | |
Anton Korobeynikov | dd9dc5d | 2008-03-11 22:38:53 +0000 | [diff] [blame] | 283 | const GlobalValue *GV = MO.getGlobal(); |
| 284 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 285 | if (!GVar) { |
Anton Korobeynikov | 8514930 | 2008-03-22 07:53:40 +0000 | [diff] [blame] | 286 | // If GV is an alias then use the aliasee for determining |
| 287 | // thread-localness. |
Anton Korobeynikov | dd9dc5d | 2008-03-11 22:38:53 +0000 | [diff] [blame] | 288 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) |
| 289 | GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal()); |
| 290 | } |
| 291 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 292 | bool isThreadLocal = GVar && GVar->isThreadLocal(); |
| 293 | |
| 294 | std::string Name = Mang->getValueName(GV); |
| 295 | X86SharedAsmPrinter::decorateName(Name, GV); |
| 296 | |
| 297 | if (!isMemOp && !isCallOp) |
| 298 | O << '$'; |
| 299 | else if (Name[0] == '$') { |
| 300 | // The name begins with a dollar-sign. In order to avoid having it look |
| 301 | // like an integer immediate to the assembler, enclose it in parens. |
| 302 | O << '('; |
| 303 | needCloseParen = true; |
| 304 | } |
| 305 | |
| 306 | if (printStub(TM, Subtarget)) { |
| 307 | // Link-once, declaration, or Weakly-linked global variables need |
| 308 | // non-lazily-resolved stubs |
| 309 | if (GV->isDeclaration() || |
| 310 | GV->hasWeakLinkage() || |
| 311 | GV->hasLinkOnceLinkage()) { |
| 312 | // Dynamically-resolved functions need a stub for the function. |
| 313 | if (isCallOp && isa<Function>(GV)) { |
| 314 | FnStubs.insert(Name); |
| 315 | O << TAI->getPrivateGlobalPrefix() << Name << "$stub"; |
| 316 | } else { |
| 317 | GVStubs.insert(Name); |
| 318 | O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr"; |
| 319 | } |
| 320 | } else { |
| 321 | if (GV->hasDLLImportLinkage()) |
| 322 | O << "__imp_"; |
| 323 | O << Name; |
| 324 | } |
| 325 | |
| 326 | if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_) |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 327 | O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 328 | } else { |
| 329 | if (GV->hasDLLImportLinkage()) { |
| 330 | O << "__imp_"; |
| 331 | } |
| 332 | O << Name; |
| 333 | |
| 334 | if (isCallOp && isa<Function>(GV)) { |
| 335 | if (printGOT(TM, Subtarget)) { |
| 336 | // Assemble call via PLT for non-local symbols |
| 337 | if (!(GV->hasHiddenVisibility() || GV->hasProtectedVisibility()) || |
| 338 | GV->isDeclaration()) |
| 339 | O << "@PLT"; |
| 340 | } |
| 341 | if (Subtarget->isTargetCygMing() && GV->isDeclaration()) |
| 342 | // Save function name for later type emission |
| 343 | FnStubs.insert(Name); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (GV->hasExternalWeakLinkage()) |
| 348 | ExtWeakSymbols.insert(GV); |
| 349 | |
| 350 | int Offset = MO.getOffset(); |
| 351 | if (Offset > 0) |
| 352 | O << "+" << Offset; |
| 353 | else if (Offset < 0) |
| 354 | O << Offset; |
| 355 | |
| 356 | if (isThreadLocal) { |
| 357 | if (TM.getRelocationModel() == Reloc::PIC_) |
| 358 | O << "@TLSGD"; // general dynamic TLS model |
| 359 | else |
| 360 | if (GV->isDeclaration()) |
| 361 | O << "@INDNTPOFF"; // initial exec TLS model |
| 362 | else |
| 363 | O << "@NTPOFF"; // local exec TLS model |
| 364 | } else if (isMemOp) { |
| 365 | if (printGOT(TM, Subtarget)) { |
| 366 | if (Subtarget->GVRequiresExtraLoad(GV, TM, false)) |
| 367 | O << "@GOT"; |
| 368 | else |
| 369 | O << "@GOTOFF"; |
Chris Lattner | fa7ef61 | 2007-11-04 19:23:28 +0000 | [diff] [blame] | 370 | } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel && |
| 371 | TM.getRelocationModel() != Reloc::Static) { |
Anton Korobeynikov | 0d38b7d | 2008-01-20 13:59:37 +0000 | [diff] [blame] | 372 | if (Subtarget->GVRequiresExtraLoad(GV, TM, false)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 373 | O << "@GOTPCREL"; |
| 374 | |
| 375 | if (needCloseParen) { |
| 376 | needCloseParen = false; |
| 377 | O << ')'; |
| 378 | } |
| 379 | |
| 380 | // Use rip when possible to reduce code size, except when |
| 381 | // index or base register are also part of the address. e.g. |
| 382 | // foo(%rip)(%rcx,%rax,4) is not legal |
| 383 | O << "(%rip)"; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (needCloseParen) |
| 388 | O << ')'; |
| 389 | |
| 390 | return; |
| 391 | } |
| 392 | case MachineOperand::MO_ExternalSymbol: { |
| 393 | bool isCallOp = Modifier && !strcmp(Modifier, "call"); |
| 394 | bool needCloseParen = false; |
| 395 | std::string Name(TAI->getGlobalPrefix()); |
| 396 | Name += MO.getSymbolName(); |
| 397 | if (isCallOp && printStub(TM, Subtarget)) { |
| 398 | FnStubs.insert(Name); |
| 399 | O << TAI->getPrivateGlobalPrefix() << Name << "$stub"; |
| 400 | return; |
| 401 | } |
| 402 | if (!isCallOp) |
| 403 | O << '$'; |
| 404 | else if (Name[0] == '$') { |
| 405 | // The name begins with a dollar-sign. In order to avoid having it look |
| 406 | // like an integer immediate to the assembler, enclose it in parens. |
| 407 | O << '('; |
| 408 | needCloseParen = true; |
| 409 | } |
| 410 | |
| 411 | O << Name; |
| 412 | |
| 413 | if (printGOT(TM, Subtarget)) { |
| 414 | std::string GOTName(TAI->getGlobalPrefix()); |
| 415 | GOTName+="_GLOBAL_OFFSET_TABLE_"; |
| 416 | if (Name == GOTName) |
| 417 | // HACK! Emit extra offset to PC during printing GOT offset to |
| 418 | // compensate for the size of popl instruction. The resulting code |
| 419 | // should look like: |
| 420 | // call .piclabel |
| 421 | // piclabel: |
| 422 | // popl %some_register |
| 423 | // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register |
| 424 | O << " + [.-" |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 425 | << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << "]"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 426 | |
| 427 | if (isCallOp) |
| 428 | O << "@PLT"; |
| 429 | } |
| 430 | |
| 431 | if (needCloseParen) |
| 432 | O << ')'; |
| 433 | |
| 434 | if (!isCallOp && Subtarget->isPICStyleRIPRel()) |
| 435 | O << "(%rip)"; |
| 436 | |
| 437 | return; |
| 438 | } |
| 439 | default: |
| 440 | O << "<unknown operand type>"; return; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 445 | unsigned char value = MI->getOperand(Op).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 446 | assert(value <= 7 && "Invalid ssecc argument!"); |
| 447 | switch (value) { |
| 448 | case 0: O << "eq"; break; |
| 449 | case 1: O << "lt"; break; |
| 450 | case 2: O << "le"; break; |
| 451 | case 3: O << "unord"; break; |
| 452 | case 4: O << "neq"; break; |
| 453 | case 5: O << "nlt"; break; |
| 454 | case 6: O << "nle"; break; |
| 455 | case 7: O << "ord"; break; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op, |
| 460 | const char *Modifier){ |
| 461 | assert(isMem(MI, Op) && "Invalid memory reference!"); |
| 462 | MachineOperand BaseReg = MI->getOperand(Op); |
| 463 | MachineOperand IndexReg = MI->getOperand(Op+2); |
| 464 | const MachineOperand &DispSpec = MI->getOperand(Op+3); |
| 465 | |
| 466 | bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg(); |
| 467 | if (DispSpec.isGlobalAddress() || |
| 468 | DispSpec.isConstantPoolIndex() || |
| 469 | DispSpec.isJumpTableIndex()) { |
| 470 | printOperand(MI, Op+3, "mem", NotRIPRel); |
| 471 | } else { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 472 | int DispVal = DispSpec.getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 473 | if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) |
| 474 | O << DispVal; |
| 475 | } |
| 476 | |
| 477 | if (IndexReg.getReg() || BaseReg.getReg()) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 478 | unsigned ScaleVal = MI->getOperand(Op+1).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 479 | unsigned BaseRegOperand = 0, IndexRegOperand = 2; |
| 480 | |
| 481 | // There are cases where we can end up with ESP/RSP in the indexreg slot. |
| 482 | // If this happens, swap the base/index register to support assemblers that |
| 483 | // don't work when the index is *SP. |
| 484 | if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) { |
| 485 | assert(ScaleVal == 1 && "Scale not supported for stack pointer!"); |
| 486 | std::swap(BaseReg, IndexReg); |
| 487 | std::swap(BaseRegOperand, IndexRegOperand); |
| 488 | } |
| 489 | |
| 490 | O << "("; |
| 491 | if (BaseReg.getReg()) |
| 492 | printOperand(MI, Op+BaseRegOperand, Modifier); |
| 493 | |
| 494 | if (IndexReg.getReg()) { |
| 495 | O << ","; |
| 496 | printOperand(MI, Op+IndexRegOperand, Modifier); |
| 497 | if (ScaleVal != 1) |
| 498 | O << "," << ScaleVal; |
| 499 | } |
| 500 | O << ")"; |
| 501 | } |
| 502 | } |
| 503 | |
Evan Cheng | 6fb0676 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 504 | void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid, |
| 505 | const MachineBasicBlock *MBB) const { |
| 506 | if (!TAI->getSetDirective()) |
| 507 | return; |
Anton Korobeynikov | 5772c67 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 508 | |
| 509 | // We don't need .set machinery if we have GOT-style relocations |
| 510 | if (Subtarget->isPICStyleGOT()) |
| 511 | return; |
| 512 | |
Evan Cheng | 6fb0676 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 513 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
| 514 | << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 515 | printBasicBlockLabel(MBB, false, false, false); |
Evan Cheng | 5da1225 | 2007-11-09 19:11:23 +0000 | [diff] [blame] | 516 | if (Subtarget->isPICStyleRIPRel()) |
| 517 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 518 | << '_' << uid << '\n'; |
| 519 | else |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 520 | O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n'; |
Evan Cheng | 6fb0676 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 523 | void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) { |
Evan Cheng | 0729ccf | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 524 | std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 525 | O << label << "\n" << label << ":"; |
| 526 | } |
| 527 | |
| 528 | |
Anton Korobeynikov | 5772c67 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 529 | void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, |
| 530 | const MachineBasicBlock *MBB, |
| 531 | unsigned uid) const |
| 532 | { |
| 533 | const char *JTEntryDirective = MJTI->getEntrySize() == 4 ? |
| 534 | TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); |
| 535 | |
| 536 | O << JTEntryDirective << ' '; |
| 537 | |
| 538 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| 539 | if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) { |
| 540 | O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() |
| 541 | << '_' << uid << "_set_" << MBB->getNumber(); |
| 542 | } else if (Subtarget->isPICStyleGOT()) { |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 543 | printBasicBlockLabel(MBB, false, false, false); |
Anton Korobeynikov | 5772c67 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 544 | O << "@GOTOFF"; |
| 545 | } else |
| 546 | assert(0 && "Don't know how to print MBB label for this PIC mode"); |
| 547 | } else |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 548 | printBasicBlockLabel(MBB, false, false, false); |
Anton Korobeynikov | 5772c67 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 551 | bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, |
| 552 | const char Mode) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 553 | unsigned Reg = MO.getReg(); |
| 554 | switch (Mode) { |
| 555 | default: return true; // Unknown mode. |
| 556 | case 'b': // Print QImode register |
| 557 | Reg = getX86SubSuperRegister(Reg, MVT::i8); |
| 558 | break; |
| 559 | case 'h': // Print QImode high register |
| 560 | Reg = getX86SubSuperRegister(Reg, MVT::i8, true); |
| 561 | break; |
| 562 | case 'w': // Print HImode register |
| 563 | Reg = getX86SubSuperRegister(Reg, MVT::i16); |
| 564 | break; |
| 565 | case 'k': // Print SImode register |
| 566 | Reg = getX86SubSuperRegister(Reg, MVT::i32); |
| 567 | break; |
Chris Lattner | 1fabfaa | 2007-10-29 03:09:07 +0000 | [diff] [blame] | 568 | case 'q': // Print DImode register |
| 569 | Reg = getX86SubSuperRegister(Reg, MVT::i64); |
| 570 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | O << '%'; |
Evan Cheng | 3c0eda5 | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 574 | for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 575 | O << (char)tolower(*Name); |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 580 | /// |
| 581 | bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 582 | unsigned AsmVariant, |
| 583 | const char *ExtraCode) { |
| 584 | // Does this asm operand have a single letter operand modifier? |
| 585 | if (ExtraCode && ExtraCode[0]) { |
| 586 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 587 | |
| 588 | switch (ExtraCode[0]) { |
| 589 | default: return true; // Unknown modifier. |
| 590 | case 'c': // Don't print "$" before a global var name or constant. |
| 591 | printOperand(MI, OpNo, "mem"); |
| 592 | return false; |
| 593 | case 'b': // Print QImode register |
| 594 | case 'h': // Print QImode high register |
| 595 | case 'w': // Print HImode register |
| 596 | case 'k': // Print SImode register |
Chris Lattner | 1fabfaa | 2007-10-29 03:09:07 +0000 | [diff] [blame] | 597 | case 'q': // Print DImode register |
Dan Gohman | 38a9a9f | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 598 | if (MI->getOperand(OpNo).isRegister()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 599 | return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]); |
| 600 | printOperand(MI, OpNo); |
| 601 | return false; |
| 602 | |
| 603 | case 'P': // Don't print @PLT, but do print as memory. |
| 604 | printOperand(MI, OpNo, "mem"); |
| 605 | return false; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | printOperand(MI, OpNo); |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 614 | unsigned OpNo, |
| 615 | unsigned AsmVariant, |
| 616 | const char *ExtraCode) { |
Chris Lattner | 1fabfaa | 2007-10-29 03:09:07 +0000 | [diff] [blame] | 617 | if (ExtraCode && ExtraCode[0]) { |
| 618 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 619 | |
| 620 | switch (ExtraCode[0]) { |
| 621 | default: return true; // Unknown modifier. |
| 622 | case 'b': // Print QImode register |
| 623 | case 'h': // Print QImode high register |
| 624 | case 'w': // Print HImode register |
| 625 | case 'k': // Print SImode register |
| 626 | case 'q': // Print SImode register |
| 627 | // These only apply to registers, ignore on mem. |
| 628 | break; |
| 629 | } |
| 630 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 631 | printMemReference(MI, OpNo); |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | /// printMachineInstruction -- Print out a single X86 LLVM instruction |
| 636 | /// MI in AT&T syntax to the current output stream. |
| 637 | /// |
| 638 | void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 639 | ++EmittedInsts; |
| 640 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 641 | // Call the autogenerated instruction printer routines. |
| 642 | printInstruction(MI); |
| 643 | } |
| 644 | |
| 645 | // Include the auto-generated portion of the assembly writer. |
| 646 | #include "X86GenAsmWriter.inc" |
| 647 | |