Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 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 the shared super class printer that converts from our internal |
| 11 | // representation of machine-dependent LLVM code to Intel and AT&T format |
| 12 | // assembly language. |
| 13 | // This printer is the output mechanism used by `llc'. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "X86AsmPrinter.h" |
| 18 | #include "X86ATTAsmPrinter.h" |
| 19 | #include "X86COFF.h" |
| 20 | #include "X86IntelAsmPrinter.h" |
| 21 | #include "X86MachineFunctionInfo.h" |
| 22 | #include "X86Subtarget.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/CallingConv.h" |
| 25 | #include "llvm/Constants.h" |
| 26 | #include "llvm/Module.h" |
| 27 | #include "llvm/DerivedTypes.h" |
Anton Korobeynikov | ab46ce6 | 2008-01-20 14:00:07 +0000 | [diff] [blame] | 28 | #include "llvm/ParameterAttributes.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | #include "llvm/Type.h" |
| 30 | #include "llvm/Assembly/Writer.h" |
| 31 | #include "llvm/Support/Mangler.h" |
| 32 | #include "llvm/Target/TargetAsmInfo.h" |
| 33 | #include "llvm/Target/TargetOptions.h" |
| 34 | using namespace llvm; |
| 35 | |
| 36 | static X86MachineFunctionInfo calculateFunctionInfo(const Function *F, |
| 37 | const TargetData *TD) { |
| 38 | X86MachineFunctionInfo Info; |
| 39 | uint64_t Size = 0; |
| 40 | |
| 41 | switch (F->getCallingConv()) { |
| 42 | case CallingConv::X86_StdCall: |
| 43 | Info.setDecorationStyle(StdCall); |
| 44 | break; |
| 45 | case CallingConv::X86_FastCall: |
| 46 | Info.setDecorationStyle(FastCall); |
| 47 | break; |
| 48 | default: |
| 49 | return Info; |
| 50 | } |
| 51 | |
Anton Korobeynikov | ab46ce6 | 2008-01-20 14:00:07 +0000 | [diff] [blame] | 52 | unsigned argNum = 1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 53 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
Anton Korobeynikov | ab46ce6 | 2008-01-20 14:00:07 +0000 | [diff] [blame] | 54 | AI != AE; ++AI, ++argNum) { |
| 55 | const Type* Ty = AI->getType(); |
| 56 | |
| 57 | // 'Dereference' type in case of byval parameter attribute |
| 58 | if (F->paramHasAttr(argNum, ParamAttr::ByVal)) |
| 59 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 60 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | // Size should be aligned to DWORD boundary |
Anton Korobeynikov | ab46ce6 | 2008-01-20 14:00:07 +0000 | [diff] [blame] | 62 | Size += ((TD->getABITypeSize(Ty) + 3)/4)*4; |
| 63 | } |
| 64 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 65 | // We're not supporting tooooo huge arguments :) |
| 66 | Info.setBytesToPopOnReturn((unsigned int)Size); |
| 67 | return Info; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /// decorateName - Query FunctionInfoMap and use this information for various |
| 72 | /// name decoration. |
| 73 | void X86SharedAsmPrinter::decorateName(std::string &Name, |
| 74 | const GlobalValue *GV) { |
| 75 | const Function *F = dyn_cast<Function>(GV); |
| 76 | if (!F) return; |
| 77 | |
| 78 | // We don't want to decorate non-stdcall or non-fastcall functions right now |
| 79 | unsigned CC = F->getCallingConv(); |
| 80 | if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall) |
| 81 | return; |
| 82 | |
| 83 | // Decorate names only when we're targeting Cygwin/Mingw32 targets |
| 84 | if (!Subtarget->isTargetCygMing()) |
| 85 | return; |
| 86 | |
| 87 | FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F); |
| 88 | |
| 89 | const X86MachineFunctionInfo *Info; |
| 90 | if (info_item == FunctionInfoMap.end()) { |
| 91 | // Calculate apropriate function info and populate map |
| 92 | FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData()); |
| 93 | Info = &FunctionInfoMap[F]; |
| 94 | } else { |
| 95 | Info = &info_item->second; |
| 96 | } |
| 97 | |
| 98 | const FunctionType *FT = F->getFunctionType(); |
| 99 | switch (Info->getDecorationStyle()) { |
| 100 | case None: |
| 101 | break; |
| 102 | case StdCall: |
| 103 | // "Pure" variadic functions do not receive @0 suffix. |
| 104 | if (!FT->isVarArg() || (FT->getNumParams() == 0) || |
Devang Patel | 949a4b7 | 2008-03-03 21:46:28 +0000 | [diff] [blame] | 105 | (FT->getNumParams() == 1 && F->hasStructRetAttr())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); |
| 107 | break; |
| 108 | case FastCall: |
| 109 | // "Pure" variadic functions do not receive @0 suffix. |
| 110 | if (!FT->isVarArg() || (FT->getNumParams() == 0) || |
Devang Patel | 949a4b7 | 2008-03-03 21:46:28 +0000 | [diff] [blame] | 111 | (FT->getNumParams() == 1 && F->hasStructRetAttr())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); |
| 113 | |
| 114 | if (Name[0] == '_') { |
| 115 | Name[0] = '@'; |
| 116 | } else { |
| 117 | Name = '@' + Name; |
| 118 | } |
| 119 | break; |
| 120 | default: |
| 121 | assert(0 && "Unsupported DecorationStyle"); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// doInitialization |
| 126 | bool X86SharedAsmPrinter::doInitialization(Module &M) { |
| 127 | if (TAI->doesSupportDebugInformation()) { |
| 128 | // Emit initial debug information. |
| 129 | DW.BeginModule(&M); |
| 130 | } |
| 131 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 132 | bool Result = AsmPrinter::doInitialization(M); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 133 | |
| 134 | // Darwin wants symbols to be quoted if they have complex names. |
| 135 | if (Subtarget->isTargetDarwin()) |
| 136 | Mang->setUseQuotes(true); |
| 137 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 138 | return Result; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Chris Lattner | 2b638a7 | 2008-02-15 19:04:54 +0000 | [diff] [blame] | 141 | /// PrintUnmangledNameSafely - Print out the printable characters in the name. |
Chris Lattner | 453b39b | 2008-02-15 18:56:05 +0000 | [diff] [blame] | 142 | /// Don't print things like \n or \0. |
Chris Lattner | 2b638a7 | 2008-02-15 19:04:54 +0000 | [diff] [blame] | 143 | static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) { |
Chris Lattner | 453b39b | 2008-02-15 18:56:05 +0000 | [diff] [blame] | 144 | for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen(); |
| 145 | Name != E; ++Name) |
| 146 | if (isprint(*Name)) |
| 147 | OS << *Name; |
| 148 | } |
| 149 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 150 | bool X86SharedAsmPrinter::doFinalization(Module &M) { |
| 151 | // Note: this code is not shared by the Intel printer as it is too different |
| 152 | // from how MASM does things. When making changes here don't forget to look |
| 153 | // at X86IntelAsmPrinter::doFinalization(). |
| 154 | const TargetData *TD = TM.getTargetData(); |
| 155 | |
| 156 | // Print out module-level global variables here. |
| 157 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 158 | I != E; ++I) { |
| 159 | if (!I->hasInitializer()) |
| 160 | continue; // External global require no code |
| 161 | |
| 162 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 163 | if (EmitSpecialLLVMGlobal(I)) { |
| 164 | if (Subtarget->isTargetDarwin() && |
| 165 | TM.getRelocationModel() == Reloc::Static) { |
| 166 | if (I->getName() == "llvm.global_ctors") |
| 167 | O << ".reference .constructors_used\n"; |
| 168 | else if (I->getName() == "llvm.global_dtors") |
| 169 | O << ".reference .destructors_used\n"; |
| 170 | } |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | std::string name = Mang->getValueName(I); |
| 175 | Constant *C = I->getInitializer(); |
| 176 | const Type *Type = C->getType(); |
Duncan Sands | 8157ef4 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 177 | unsigned Size = TD->getABITypeSize(Type); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 178 | unsigned Align = TD->getPreferredAlignmentLog(I); |
| 179 | |
| 180 | if (I->hasHiddenVisibility()) { |
| 181 | if (const char *Directive = TAI->getHiddenDirective()) |
| 182 | O << Directive << name << "\n"; |
| 183 | } else if (I->hasProtectedVisibility()) { |
| 184 | if (const char *Directive = TAI->getProtectedDirective()) |
| 185 | O << Directive << name << "\n"; |
| 186 | } |
| 187 | |
| 188 | if (Subtarget->isTargetELF()) |
Dan Gohman | 721e658 | 2007-07-30 15:08:02 +0000 | [diff] [blame] | 189 | O << "\t.type\t" << name << ",@object\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 190 | |
Evan Cheng | 65c0fbc | 2007-09-21 00:41:19 +0000 | [diff] [blame] | 191 | if (C->isNullValue() && !I->hasSection()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 192 | if (I->hasExternalLinkage()) { |
| 193 | if (const char *Directive = TAI->getZeroFillDirective()) { |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 194 | O << "\t.globl " << name << "\n"; |
Dale Johannesen | 576dc02 | 2008-02-12 23:35:09 +0000 | [diff] [blame] | 195 | O << Directive << "__DATA, __common, " << name << ", " |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | << Size << ", " << Align << "\n"; |
| 197 | continue; |
| 198 | } |
| 199 | } |
| 200 | |
Evan Cheng | 65c0fbc | 2007-09-21 00:41:19 +0000 | [diff] [blame] | 201 | if (!I->isThreadLocal() && |
Dale Johannesen | 50085da | 2008-01-17 23:04:07 +0000 | [diff] [blame] | 202 | (I->hasInternalLinkage() || I->hasWeakLinkage() || |
| 203 | I->hasLinkOnceLinkage())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 204 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 205 | if (!NoZerosInBSS && TAI->getBSSSection()) |
| 206 | SwitchToDataSection(TAI->getBSSSection(), I); |
| 207 | else |
| 208 | SwitchToDataSection(TAI->getDataSection(), I); |
| 209 | if (TAI->getLCOMMDirective() != NULL) { |
| 210 | if (I->hasInternalLinkage()) { |
| 211 | O << TAI->getLCOMMDirective() << name << "," << Size; |
| 212 | if (Subtarget->isTargetDarwin()) |
| 213 | O << "," << Align; |
Chris Lattner | 93a2d43 | 2008-01-02 19:44:55 +0000 | [diff] [blame] | 214 | } else { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 215 | O << TAI->getCOMMDirective() << name << "," << Size; |
Chris Lattner | 93a2d43 | 2008-01-02 19:44:55 +0000 | [diff] [blame] | 216 | |
| 217 | // Leopard and above support aligned common symbols. |
| 218 | if (Subtarget->getDarwinVers() >= 9) |
| 219 | O << "," << Align; |
| 220 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 221 | } else { |
| 222 | if (!Subtarget->isTargetCygMing()) { |
| 223 | if (I->hasInternalLinkage()) |
| 224 | O << "\t.local\t" << name << "\n"; |
| 225 | } |
| 226 | O << TAI->getCOMMDirective() << name << "," << Size; |
| 227 | if (TAI->getCOMMDirectiveTakesAlignment()) |
| 228 | O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align); |
| 229 | } |
Chris Lattner | 453b39b | 2008-02-15 18:56:05 +0000 | [diff] [blame] | 230 | O << "\t\t" << TAI->getCommentString() << " "; |
Chris Lattner | 2b638a7 | 2008-02-15 19:04:54 +0000 | [diff] [blame] | 231 | PrintUnmangledNameSafely(I, O); |
Chris Lattner | 453b39b | 2008-02-15 18:56:05 +0000 | [diff] [blame] | 232 | O << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | continue; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | switch (I->getLinkage()) { |
| 238 | case GlobalValue::LinkOnceLinkage: |
| 239 | case GlobalValue::WeakLinkage: |
| 240 | if (Subtarget->isTargetDarwin()) { |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 241 | O << "\t.globl " << name << "\n" |
Dale Johannesen | fb3ac73 | 2007-11-20 23:24:42 +0000 | [diff] [blame] | 242 | << TAI->getWeakDefDirective() << name << "\n"; |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 243 | SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 244 | } else if (Subtarget->isTargetCygMing()) { |
| 245 | std::string SectionName(".section\t.data$linkonce." + |
| 246 | name + |
| 247 | ",\"aw\""); |
| 248 | SwitchToDataSection(SectionName.c_str(), I); |
Dan Gohman | 52443a8 | 2007-10-05 15:58:41 +0000 | [diff] [blame] | 249 | O << "\t.globl\t" << name << "\n" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 250 | << "\t.linkonce same_size\n"; |
| 251 | } else { |
| 252 | std::string SectionName("\t.section\t.llvm.linkonce.d." + |
| 253 | name + |
| 254 | ",\"aw\",@progbits"); |
| 255 | SwitchToDataSection(SectionName.c_str(), I); |
Dan Gohman | 721e658 | 2007-07-30 15:08:02 +0000 | [diff] [blame] | 256 | O << "\t.weak\t" << name << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 257 | } |
| 258 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 259 | case GlobalValue::DLLExportLinkage: |
| 260 | DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),"")); |
| 261 | // FALL THROUGH |
Chris Lattner | 2f8ba29 | 2007-08-13 18:42:37 +0000 | [diff] [blame] | 262 | case GlobalValue::AppendingLinkage: |
| 263 | // FIXME: appending linkage variables should go into a section of |
| 264 | // their name or something. For now, just emit them as external. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 265 | case GlobalValue::ExternalLinkage: |
| 266 | // If external or appending, declare as a global symbol |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 267 | O << "\t.globl " << name << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 268 | // FALL THROUGH |
| 269 | case GlobalValue::InternalLinkage: { |
| 270 | if (I->isConstant()) { |
| 271 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 272 | if (TAI->getCStringSection() && CVA && CVA->isCString()) { |
| 273 | SwitchToDataSection(TAI->getCStringSection(), I); |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | // FIXME: special handling for ".ctors" & ".dtors" sections |
| 278 | if (I->hasSection() && |
| 279 | (I->getSection() == ".ctors" || |
| 280 | I->getSection() == ".dtors")) { |
| 281 | std::string SectionName = ".section " + I->getSection(); |
| 282 | |
| 283 | if (Subtarget->isTargetCygMing()) { |
| 284 | SectionName += ",\"aw\""; |
| 285 | } else { |
| 286 | assert(!Subtarget->isTargetDarwin()); |
| 287 | SectionName += ",\"aw\",@progbits"; |
| 288 | } |
Dale Johannesen | ae4f62f | 2008-01-23 00:58:14 +0000 | [diff] [blame] | 289 | SwitchToDataSection(SectionName.c_str()); |
| 290 | } else if (I->hasSection() && Subtarget->isTargetDarwin()) { |
| 291 | // Honor all section names on Darwin; ObjC uses this |
| 292 | std::string SectionName = ".section " + I->getSection(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 293 | SwitchToDataSection(SectionName.c_str()); |
| 294 | } else { |
| 295 | if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection()) |
| 296 | SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() : |
| 297 | TAI->getBSSSection(), I); |
| 298 | else if (!I->isConstant()) |
| 299 | SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() : |
| 300 | TAI->getDataSection(), I); |
| 301 | else if (I->isThreadLocal()) |
| 302 | SwitchToDataSection(TAI->getTLSDataSection()); |
| 303 | else { |
| 304 | // Read-only data. |
| 305 | bool HasReloc = C->ContainsRelocations(); |
| 306 | if (HasReloc && |
| 307 | Subtarget->isTargetDarwin() && |
| 308 | TM.getRelocationModel() != Reloc::Static) |
| 309 | SwitchToDataSection("\t.const_data\n"); |
| 310 | else if (!HasReloc && Size == 4 && |
| 311 | TAI->getFourByteConstantSection()) |
| 312 | SwitchToDataSection(TAI->getFourByteConstantSection(), I); |
| 313 | else if (!HasReloc && Size == 8 && |
| 314 | TAI->getEightByteConstantSection()) |
| 315 | SwitchToDataSection(TAI->getEightByteConstantSection(), I); |
| 316 | else if (!HasReloc && Size == 16 && |
| 317 | TAI->getSixteenByteConstantSection()) |
| 318 | SwitchToDataSection(TAI->getSixteenByteConstantSection(), I); |
| 319 | else if (TAI->getReadOnlySection()) |
| 320 | SwitchToDataSection(TAI->getReadOnlySection(), I); |
| 321 | else |
| 322 | SwitchToDataSection(TAI->getDataSection(), I); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | break; |
| 327 | } |
| 328 | default: |
| 329 | assert(0 && "Unknown linkage type!"); |
| 330 | } |
| 331 | |
| 332 | EmitAlignment(Align, I); |
Chris Lattner | 453b39b | 2008-02-15 18:56:05 +0000 | [diff] [blame] | 333 | O << name << ":\t\t\t\t" << TAI->getCommentString() << " "; |
Chris Lattner | 2b638a7 | 2008-02-15 19:04:54 +0000 | [diff] [blame] | 334 | PrintUnmangledNameSafely(I, O); |
Chris Lattner | 453b39b | 2008-02-15 18:56:05 +0000 | [diff] [blame] | 335 | O << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 336 | if (TAI->hasDotTypeDotSizeDirective()) |
Dan Gohman | 721e658 | 2007-07-30 15:08:02 +0000 | [diff] [blame] | 337 | O << "\t.size\t" << name << ", " << Size << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 338 | // If the initializer is a extern weak symbol, remember to emit the weak |
| 339 | // reference! |
| 340 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 341 | if (GV->hasExternalWeakLinkage()) |
| 342 | ExtWeakSymbols.insert(GV); |
| 343 | |
| 344 | EmitGlobalConstant(C); |
| 345 | } |
| 346 | |
| 347 | // Output linker support code for dllexported globals |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 348 | if (!DLLExportedGVs.empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 349 | SwitchToDataSection(".section .drectve"); |
| 350 | } |
| 351 | |
| 352 | for (std::set<std::string>::iterator i = DLLExportedGVs.begin(), |
| 353 | e = DLLExportedGVs.end(); |
| 354 | i != e; ++i) { |
| 355 | O << "\t.ascii \" -export:" << *i << ",data\"\n"; |
| 356 | } |
| 357 | |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 358 | if (!DLLExportedFns.empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 359 | SwitchToDataSection(".section .drectve"); |
| 360 | } |
| 361 | |
| 362 | for (std::set<std::string>::iterator i = DLLExportedFns.begin(), |
| 363 | e = DLLExportedFns.end(); |
| 364 | i != e; ++i) { |
| 365 | O << "\t.ascii \" -export:" << *i << "\"\n"; |
| 366 | } |
| 367 | |
| 368 | if (Subtarget->isTargetDarwin()) { |
| 369 | SwitchToDataSection(""); |
| 370 | |
| 371 | // Output stubs for dynamically-linked functions |
| 372 | unsigned j = 1; |
| 373 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 374 | i != e; ++i, ++j) { |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 375 | SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs," |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 376 | "self_modifying_code+pure_instructions,5", 0); |
| 377 | O << "L" << *i << "$stub:\n"; |
| 378 | O << "\t.indirect_symbol " << *i << "\n"; |
| 379 | O << "\thlt ; hlt ; hlt ; hlt ; hlt\n"; |
| 380 | } |
| 381 | |
| 382 | O << "\n"; |
| 383 | |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 384 | if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) { |
Bill Wendling | d1bda4f | 2007-09-11 08:27:17 +0000 | [diff] [blame] | 385 | // Add the (possibly multiple) personalities to the set of global values. |
Dale Johannesen | 8553576 | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 386 | // Only referenced functions get into the Personalities list. |
Bill Wendling | d1bda4f | 2007-09-11 08:27:17 +0000 | [diff] [blame] | 387 | const std::vector<Function *>& Personalities = MMI->getPersonalities(); |
| 388 | |
| 389 | for (std::vector<Function *>::const_iterator I = Personalities.begin(), |
| 390 | E = Personalities.end(); I != E; ++I) |
| 391 | if (*I) GVStubs.insert("_" + (*I)->getName()); |
| 392 | } |
| 393 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 394 | // Output stubs for external and common global variables. |
Dan Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 395 | if (!GVStubs.empty()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 396 | SwitchToDataSection( |
Dale Johannesen | 3c78832 | 2008-01-11 00:54:37 +0000 | [diff] [blame] | 397 | "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 398 | for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); |
| 399 | i != e; ++i) { |
| 400 | O << "L" << *i << "$non_lazy_ptr:\n"; |
| 401 | O << "\t.indirect_symbol " << *i << "\n"; |
| 402 | O << "\t.long\t0\n"; |
| 403 | } |
| 404 | |
| 405 | // Emit final debug information. |
| 406 | DW.EndModule(); |
| 407 | |
| 408 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 409 | // contain code that falls through to other global symbols (e.g. the obvious |
| 410 | // implementation of multiple entry points). If this doesn't occur, the |
| 411 | // linker can safely perform dead code stripping. Since LLVM never |
| 412 | // generates code that does this, it is always safe to set. |
| 413 | O << "\t.subsections_via_symbols\n"; |
| 414 | } else if (Subtarget->isTargetCygMing()) { |
| 415 | // Emit type information for external functions |
| 416 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 417 | i != e; ++i) { |
| 418 | O << "\t.def\t " << *i |
| 419 | << ";\t.scl\t" << COFF::C_EXT |
| 420 | << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) |
| 421 | << ";\t.endef\n"; |
| 422 | } |
| 423 | |
| 424 | // Emit final debug information. |
| 425 | DW.EndModule(); |
| 426 | } else if (Subtarget->isTargetELF()) { |
| 427 | // Emit final debug information. |
| 428 | DW.EndModule(); |
| 429 | } |
| 430 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 431 | return AsmPrinter::doFinalization(M); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code |
| 435 | /// for a MachineFunction to the given output stream, using the given target |
| 436 | /// machine description. |
| 437 | /// |
| 438 | FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o, |
| 439 | X86TargetMachine &tm) { |
| 440 | const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>(); |
| 441 | |
| 442 | if (Subtarget->isFlavorIntel()) { |
| 443 | return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
| 444 | } else { |
| 445 | return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
| 446 | } |
| 447 | } |