Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 1 | //===-- X86AsmPrinter.cpp - Convert X86 LLVM IR to X86 assembly -----------===// |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 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'. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 17 | #include "X86AsmPrinter.h" |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 18 | #include "X86ATTAsmPrinter.h" |
Anton Korobeynikov | d05ca65 | 2007-01-16 16:41:57 +0000 | [diff] [blame] | 19 | #include "X86COFF.h" |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 20 | #include "X86IntelAsmPrinter.h" |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 21 | #include "X86MachineFunctionInfo.h" |
Chris Lattner | a35a8e8 | 2005-11-21 22:39:40 +0000 | [diff] [blame] | 22 | #include "X86Subtarget.h" |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/CallingConv.h" |
Chris Lattner | a046e0d | 2005-12-13 04:53:51 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | d59414f | 2003-08-11 20:06:16 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
Anton Korobeynikov | ad7baee | 2007-03-31 13:11:52 +0000 | [diff] [blame] | 27 | #include "llvm/DerivedTypes.h" |
Chris Lattner | c41cc83 | 2005-11-21 06:46:22 +0000 | [diff] [blame] | 28 | #include "llvm/Type.h" |
Chris Lattner | d59414f | 2003-08-11 20:06:16 +0000 | [diff] [blame] | 29 | #include "llvm/Assembly/Writer.h" |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Mangler.h" |
Jim Laskey | a0f3d17 | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetAsmInfo.h" |
Anton Korobeynikov | 5032e5a | 2007-01-17 10:33:08 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 300d0ed | 2004-02-14 06:00:36 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | d15dff2 | 2007-04-17 17:21:52 +0000 | [diff] [blame^] | 35 | static X86MachineFunctionInfo calculateFunctionInfo(const Function *F, |
| 36 | const TargetData *TD) { |
| 37 | X86MachineFunctionInfo Info; |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 38 | uint64_t Size = 0; |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 39 | |
| 40 | switch (F->getCallingConv()) { |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 41 | case CallingConv::X86_StdCall: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 42 | Info.setDecorationStyle(StdCall); |
| 43 | break; |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 44 | case CallingConv::X86_FastCall: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 45 | Info.setDecorationStyle(FastCall); |
| 46 | break; |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 47 | default: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 48 | return Info; |
| 49 | } |
| 50 | |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 51 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 52 | AI != AE; ++AI) |
Anton Korobeynikov | 9dd9abd | 2007-03-01 16:29:22 +0000 | [diff] [blame] | 53 | // Size should be aligned to DWORD boundary |
| 54 | Size += ((TD->getTypeSize(AI->getType()) + 3)/4)*4; |
Anton Korobeynikov | 54b1cc6 | 2006-10-14 20:53:35 +0000 | [diff] [blame] | 55 | |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 56 | // We're not supporting tooooo huge arguments :) |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 57 | Info.setBytesToPopOnReturn((unsigned int)Size); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 58 | return Info; |
| 59 | } |
| 60 | |
| 61 | |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 62 | /// decorateName - Query FunctionInfoMap and use this information for various |
| 63 | /// name decoration. |
| 64 | void X86SharedAsmPrinter::decorateName(std::string &Name, |
| 65 | const GlobalValue *GV) { |
| 66 | const Function *F = dyn_cast<Function>(GV); |
| 67 | if (!F) return; |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 68 | |
| 69 | // We don't want to decorate non-stdcall or non-fastcall functions right now |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 70 | unsigned CC = F->getCallingConv(); |
| 71 | if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall) |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 72 | return; |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 73 | |
| 74 | // Decorate names only when we're targeting Cygwin/Mingw32 targets |
| 75 | if (!Subtarget->isTargetCygMing()) |
| 76 | return; |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 77 | |
| 78 | FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F); |
| 79 | |
Chris Lattner | d15dff2 | 2007-04-17 17:21:52 +0000 | [diff] [blame^] | 80 | const X86MachineFunctionInfo *Info; |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 81 | if (info_item == FunctionInfoMap.end()) { |
| 82 | // Calculate apropriate function info and populate map |
| 83 | FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData()); |
| 84 | Info = &FunctionInfoMap[F]; |
| 85 | } else { |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 86 | Info = &info_item->second; |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 87 | } |
Anton Korobeynikov | ad7baee | 2007-03-31 13:11:52 +0000 | [diff] [blame] | 88 | |
| 89 | const FunctionType *FT = F->getFunctionType(); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 90 | switch (Info->getDecorationStyle()) { |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 91 | case None: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 92 | break; |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 93 | case StdCall: |
Anton Korobeynikov | ad7baee | 2007-03-31 13:11:52 +0000 | [diff] [blame] | 94 | // "Pure" variadic functions do not receive @0 suffix. |
| 95 | if (!FT->isVarArg() || (FT->getNumParams() == 0) || |
| 96 | (FT->getNumParams() == 1 && FT->isStructReturn())) |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 97 | Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 98 | break; |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 99 | case FastCall: |
Anton Korobeynikov | ad7baee | 2007-03-31 13:11:52 +0000 | [diff] [blame] | 100 | // "Pure" variadic functions do not receive @0 suffix. |
| 101 | if (!FT->isVarArg() || (FT->getNumParams() == 0) || |
| 102 | (FT->getNumParams() == 1 && FT->isStructReturn())) |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 103 | Name += '@' + utostr_32(Info->getBytesToPopOnReturn()); |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 104 | |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 105 | if (Name[0] == '_') { |
| 106 | Name[0] = '@'; |
| 107 | } else { |
| 108 | Name = '@' + Name; |
| 109 | } |
| 110 | break; |
Chris Lattner | e87e115 | 2006-09-26 03:57:53 +0000 | [diff] [blame] | 111 | default: |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 112 | assert(0 && "Unsupported DecorationStyle"); |
| 113 | } |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 116 | /// doInitialization |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 117 | bool X86SharedAsmPrinter::doInitialization(Module &M) { |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 118 | if (Subtarget->isTargetELF() || |
| 119 | Subtarget->isTargetCygMing() || |
| 120 | Subtarget->isTargetDarwin()) { |
Reid Spencer | 02b8511 | 2006-10-30 22:32:30 +0000 | [diff] [blame] | 121 | // Emit initial debug information. |
| 122 | DW.BeginModule(&M); |
Evan Cheng | d594881 | 2006-03-07 02:23:26 +0000 | [diff] [blame] | 123 | } |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 124 | |
Reid Spencer | 5dc81f6 | 2005-01-23 03:52:14 +0000 | [diff] [blame] | 125 | return AsmPrinter::doInitialization(M); |
| 126 | } |
| 127 | |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 128 | bool X86SharedAsmPrinter::doFinalization(Module &M) { |
Jeff Cohen | d43b18d | 2006-05-06 21:27:14 +0000 | [diff] [blame] | 129 | // Note: this code is not shared by the Intel printer as it is too different |
| 130 | // from how MASM does things. When making changes here don't forget to look |
| 131 | // at X86IntelAsmPrinter::doFinalization(). |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 132 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 133 | |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 134 | // Print out module-level global variables here. |
Evan Cheng | 2338c5c | 2006-02-07 08:38:37 +0000 | [diff] [blame] | 135 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 136 | I != E; ++I) { |
Rafael Espindola | 3e69a7e | 2006-12-09 23:14:08 +0000 | [diff] [blame] | 137 | if (!I->hasInitializer()) |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 138 | continue; // External global require no code |
Chris Lattner | 9787c64 | 2005-11-21 22:48:18 +0000 | [diff] [blame] | 139 | |
Chris Lattner | d1239b7 | 2005-12-13 06:32:50 +0000 | [diff] [blame] | 140 | // Check to see if this is a special global used by LLVM, if so, emit it. |
Evan Cheng | b267ca1 | 2007-01-30 08:04:53 +0000 | [diff] [blame] | 141 | if (EmitSpecialLLVMGlobal(I)) { |
| 142 | if (Subtarget->isTargetDarwin() && |
| 143 | TM.getRelocationModel() == Reloc::Static) { |
| 144 | if (I->getName() == "llvm.global_ctors") |
| 145 | O << ".reference .constructors_used\n"; |
| 146 | else if (I->getName() == "llvm.global_dtors") |
| 147 | O << ".reference .destructors_used\n"; |
| 148 | } |
Chris Lattner | d1239b7 | 2005-12-13 06:32:50 +0000 | [diff] [blame] | 149 | continue; |
Evan Cheng | b267ca1 | 2007-01-30 08:04:53 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | a046e0d | 2005-12-13 04:53:51 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 9787c64 | 2005-11-21 22:48:18 +0000 | [diff] [blame] | 152 | std::string name = Mang->getValueName(I); |
| 153 | Constant *C = I->getInitializer(); |
Evan Cheng | f0b5d56 | 2007-03-08 01:07:07 +0000 | [diff] [blame] | 154 | const Type *Type = C->getType(); |
| 155 | unsigned Size = TD->getTypeSize(Type); |
Devang Patel | f9c197e | 2006-10-24 20:32:14 +0000 | [diff] [blame] | 156 | unsigned Align = TD->getPreferredAlignmentLog(I); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 158 | if (I->hasHiddenVisibility()) |
| 159 | if (const char *Directive = TAI->getHiddenDirective()) |
| 160 | O << Directive << name << "\n"; |
| 161 | if (Subtarget->isTargetELF()) |
| 162 | O << "\t.type " << name << ",@object\n"; |
| 163 | |
| 164 | if (C->isNullValue()) { |
Evan Cheng | 17ef92e | 2006-02-15 01:56:23 +0000 | [diff] [blame] | 165 | if (I->hasExternalLinkage()) { |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 166 | if (const char *Directive = TAI->getZeroFillDirective()) { |
Evan Cheng | a0ea053 | 2006-02-23 02:43:52 +0000 | [diff] [blame] | 167 | O << "\t.globl\t" << name << "\n"; |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 168 | O << Directive << "__DATA__, __common, " << name << ", " |
Bill Wendling | 39e9c09 | 2007-01-18 02:30:19 +0000 | [diff] [blame] | 169 | << Size << ", " << Align << "\n"; |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 170 | continue; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (!I->hasSection() && |
| 175 | (I->hasInternalLinkage() || I->hasWeakLinkage() || |
| 176 | I->hasLinkOnceLinkage())) { |
| 177 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
Anton Korobeynikov | 5032e5a | 2007-01-17 10:33:08 +0000 | [diff] [blame] | 178 | if (!NoZerosInBSS && TAI->getBSSSection()) |
| 179 | SwitchToDataSection(TAI->getBSSSection(), I); |
| 180 | else |
| 181 | SwitchToDataSection(TAI->getDataSection(), I); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 182 | if (TAI->getLCOMMDirective() != NULL) { |
Evan Cheng | 17ef92e | 2006-02-15 01:56:23 +0000 | [diff] [blame] | 183 | if (I->hasInternalLinkage()) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 184 | O << TAI->getLCOMMDirective() << name << "," << Size; |
Jim Laskey | ea34858 | 2006-07-27 02:05:13 +0000 | [diff] [blame] | 185 | if (Subtarget->isTargetDarwin()) |
Evan Cheng | 071b9d5 | 2007-01-18 01:49:58 +0000 | [diff] [blame] | 186 | O << "," << Align; |
Evan Cheng | 17ef92e | 2006-02-15 01:56:23 +0000 | [diff] [blame] | 187 | } else |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 188 | O << TAI->getCOMMDirective() << name << "," << Size; |
Evan Cheng | 17ef92e | 2006-02-15 01:56:23 +0000 | [diff] [blame] | 189 | } else { |
Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 190 | if (!Subtarget->isTargetCygMing()) { |
Evan Cheng | 932ad51 | 2006-05-25 21:59:08 +0000 | [diff] [blame] | 191 | if (I->hasInternalLinkage()) |
| 192 | O << "\t.local\t" << name << "\n"; |
| 193 | } |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 194 | O << TAI->getCOMMDirective() << name << "," << Size; |
| 195 | if (TAI->getCOMMDirectiveTakesAlignment()) |
| 196 | O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align); |
Evan Cheng | 17ef92e | 2006-02-15 01:56:23 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 198 | O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n"; |
| 199 | continue; |
Chris Lattner | b12c9fa | 2005-07-11 06:25:47 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | 9787c64 | 2005-11-21 22:48:18 +0000 | [diff] [blame] | 201 | } |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 202 | |
| 203 | switch (I->getLinkage()) { |
| 204 | case GlobalValue::LinkOnceLinkage: |
| 205 | case GlobalValue::WeakLinkage: |
| 206 | if (Subtarget->isTargetDarwin()) { |
| 207 | O << "\t.globl " << name << "\n" |
| 208 | << "\t.weak_definition " << name << "\n"; |
| 209 | SwitchToDataSection(".section __DATA,__const_coal,coalesced", I); |
| 210 | } else if (Subtarget->isTargetCygMing()) { |
| 211 | std::string SectionName(".section\t.data$linkonce." + |
| 212 | name + |
| 213 | ",\"aw\""); |
| 214 | SwitchToDataSection(SectionName.c_str(), I); |
| 215 | O << "\t.globl " << name << "\n" |
| 216 | << "\t.linkonce same_size\n"; |
| 217 | } else { |
| 218 | std::string SectionName("\t.section\t.llvm.linkonce.d." + |
| 219 | name + |
| 220 | ",\"aw\",@progbits"); |
| 221 | SwitchToDataSection(SectionName.c_str(), I); |
| 222 | O << "\t.weak " << name << "\n"; |
| 223 | } |
| 224 | break; |
| 225 | case GlobalValue::AppendingLinkage: |
| 226 | // FIXME: appending linkage variables should go into a section of |
| 227 | // their name or something. For now, just emit them as external. |
| 228 | case GlobalValue::DLLExportLinkage: |
| 229 | DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),"")); |
| 230 | // FALL THROUGH |
| 231 | case GlobalValue::ExternalLinkage: |
| 232 | // If external or appending, declare as a global symbol |
| 233 | O << "\t.globl " << name << "\n"; |
| 234 | // FALL THROUGH |
| 235 | case GlobalValue::InternalLinkage: { |
| 236 | if (I->isConstant()) { |
| 237 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 238 | if (TAI->getCStringSection() && CVA && CVA->isCString()) { |
| 239 | SwitchToDataSection(TAI->getCStringSection(), I); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | // FIXME: special handling for ".ctors" & ".dtors" sections |
| 244 | if (I->hasSection() && |
| 245 | (I->getSection() == ".ctors" || |
| 246 | I->getSection() == ".dtors")) { |
| 247 | std::string SectionName = ".section " + I->getSection(); |
| 248 | |
| 249 | if (Subtarget->isTargetCygMing()) { |
| 250 | SectionName += ",\"aw\""; |
| 251 | } else { |
| 252 | assert(!Subtarget->isTargetDarwin()); |
| 253 | SectionName += ",\"aw\",@progbits"; |
| 254 | } |
| 255 | |
| 256 | SwitchToDataSection(SectionName.c_str()); |
| 257 | } else { |
| 258 | if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection()) |
| 259 | SwitchToDataSection(TAI->getBSSSection(), I); |
Evan Cheng | f0b5d56 | 2007-03-08 01:07:07 +0000 | [diff] [blame] | 260 | else if (!I->isConstant()) |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 261 | SwitchToDataSection(TAI->getDataSection(), I); |
Evan Cheng | f0b5d56 | 2007-03-08 01:07:07 +0000 | [diff] [blame] | 262 | else { |
| 263 | // Read-only data. |
Evan Cheng | 032953d | 2007-03-08 08:31:54 +0000 | [diff] [blame] | 264 | bool HasReloc = C->ContainsRelocations(); |
| 265 | if (HasReloc && |
| 266 | Subtarget->isTargetDarwin() && |
Evan Cheng | f0b5d56 | 2007-03-08 01:07:07 +0000 | [diff] [blame] | 267 | TM.getRelocationModel() != Reloc::Static) |
| 268 | SwitchToDataSection("\t.const_data\n"); |
Evan Cheng | 032953d | 2007-03-08 08:31:54 +0000 | [diff] [blame] | 269 | else if (!HasReloc && Size == 4 && |
Evan Cheng | f0b5d56 | 2007-03-08 01:07:07 +0000 | [diff] [blame] | 270 | TAI->getFourByteConstantSection()) |
| 271 | SwitchToDataSection(TAI->getFourByteConstantSection(), I); |
Evan Cheng | 032953d | 2007-03-08 08:31:54 +0000 | [diff] [blame] | 272 | else if (!HasReloc && Size == 8 && |
Evan Cheng | f0b5d56 | 2007-03-08 01:07:07 +0000 | [diff] [blame] | 273 | TAI->getEightByteConstantSection()) |
| 274 | SwitchToDataSection(TAI->getEightByteConstantSection(), I); |
Evan Cheng | 032953d | 2007-03-08 08:31:54 +0000 | [diff] [blame] | 275 | else if (!HasReloc && Size == 16 && |
Evan Cheng | f0b5d56 | 2007-03-08 01:07:07 +0000 | [diff] [blame] | 276 | TAI->getSixteenByteConstantSection()) |
| 277 | SwitchToDataSection(TAI->getSixteenByteConstantSection(), I); |
| 278 | else if (TAI->getReadOnlySection()) |
| 279 | SwitchToDataSection(TAI->getReadOnlySection(), I); |
| 280 | else |
| 281 | SwitchToDataSection(TAI->getDataSection(), I); |
| 282 | } |
Chris Lattner | 8e13e90 | 2007-01-17 17:44:25 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | break; |
| 286 | } |
| 287 | default: |
| 288 | assert(0 && "Unknown linkage type!"); |
| 289 | } |
| 290 | |
| 291 | EmitAlignment(Align, I); |
| 292 | O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName() |
| 293 | << "\n"; |
| 294 | if (TAI->hasDotTypeDotSizeDirective()) |
| 295 | O << "\t.size " << name << ", " << Size << "\n"; |
| 296 | // If the initializer is a extern weak symbol, remember to emit the weak |
| 297 | // reference! |
| 298 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 299 | if (GV->hasExternalWeakLinkage()) |
| 300 | ExtWeakSymbols.insert(GV); |
| 301 | |
| 302 | EmitGlobalConstant(C); |
| 303 | O << '\n'; |
Chris Lattner | a35a8e8 | 2005-11-21 22:39:40 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 306 | // Output linker support code for dllexported globals |
| 307 | if (DLLExportedGVs.begin() != DLLExportedGVs.end()) { |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 308 | SwitchToDataSection(".section .drectve"); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | for (std::set<std::string>::iterator i = DLLExportedGVs.begin(), |
| 312 | e = DLLExportedGVs.end(); |
| 313 | i != e; ++i) { |
| 314 | O << "\t.ascii \" -export:" << *i << ",data\"\n"; |
| 315 | } |
| 316 | |
| 317 | if (DLLExportedFns.begin() != DLLExportedFns.end()) { |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 318 | SwitchToDataSection(".section .drectve"); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | for (std::set<std::string>::iterator i = DLLExportedFns.begin(), |
| 322 | e = DLLExportedFns.end(); |
| 323 | i != e; ++i) { |
| 324 | O << "\t.ascii \" -export:" << *i << "\"\n"; |
| 325 | } |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 326 | |
Jim Laskey | ea34858 | 2006-07-27 02:05:13 +0000 | [diff] [blame] | 327 | if (Subtarget->isTargetDarwin()) { |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 328 | SwitchToDataSection(""); |
Nate Begeman | 73213f6 | 2005-07-12 01:37:28 +0000 | [diff] [blame] | 329 | |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 330 | // Output stubs for dynamically-linked functions |
| 331 | unsigned j = 1; |
| 332 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
Chris Lattner | b12c9fa | 2005-07-11 06:25:47 +0000 | [diff] [blame] | 333 | i != e; ++i, ++j) { |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 334 | SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs," |
| 335 | "self_modifying_code+pure_instructions,5", 0); |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 336 | O << "L" << *i << "$stub:\n"; |
| 337 | O << "\t.indirect_symbol " << *i << "\n"; |
Evan Cheng | 2338c5c | 2006-02-07 08:38:37 +0000 | [diff] [blame] | 338 | O << "\thlt ; hlt ; hlt ; hlt ; hlt\n"; |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | O << "\n"; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 342 | |
Evan Cheng | 2338c5c | 2006-02-07 08:38:37 +0000 | [diff] [blame] | 343 | // Output stubs for external and common global variables. |
| 344 | if (GVStubs.begin() != GVStubs.end()) |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 345 | SwitchToDataSection( |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 346 | ".section __IMPORT,__pointers,non_lazy_symbol_pointers"); |
Evan Cheng | 2338c5c | 2006-02-07 08:38:37 +0000 | [diff] [blame] | 347 | for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); |
| 348 | i != e; ++i) { |
| 349 | O << "L" << *i << "$non_lazy_ptr:\n"; |
| 350 | O << "\t.indirect_symbol " << *i << "\n"; |
| 351 | O << "\t.long\t0\n"; |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 352 | } |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 353 | |
Reid Spencer | 02b8511 | 2006-10-30 22:32:30 +0000 | [diff] [blame] | 354 | // Emit final debug information. |
Jim Laskey | 99db044 | 2006-03-23 18:09:44 +0000 | [diff] [blame] | 355 | DW.EndModule(); |
Evan Cheng | d594881 | 2006-03-07 02:23:26 +0000 | [diff] [blame] | 356 | |
| 357 | // Funny Darwin hack: This flag tells the linker that no global symbols |
| 358 | // contain code that falls through to other global symbols (e.g. the obvious |
| 359 | // implementation of multiple entry points). If this doesn't occur, the |
Jim Laskey | 99db044 | 2006-03-23 18:09:44 +0000 | [diff] [blame] | 360 | // linker can safely perform dead code stripping. Since LLVM never |
| 361 | // generates code that does this, it is always safe to set. |
Evan Cheng | d594881 | 2006-03-07 02:23:26 +0000 | [diff] [blame] | 362 | O << "\t.subsections_via_symbols\n"; |
Anton Korobeynikov | d05ca65 | 2007-01-16 16:41:57 +0000 | [diff] [blame] | 363 | } else if (Subtarget->isTargetCygMing()) { |
| 364 | // Emit type information for external functions |
| 365 | for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); |
| 366 | i != e; ++i) { |
| 367 | O << "\t.def\t " << *i |
| 368 | << ";\t.scl\t" << COFF::C_EXT |
| 369 | << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) |
| 370 | << ";\t.endef\n"; |
| 371 | } |
| 372 | |
| 373 | // Emit final debug information. |
| 374 | DW.EndModule(); |
| 375 | } else if (Subtarget->isTargetELF()) { |
Reid Spencer | 02b8511 | 2006-10-30 22:32:30 +0000 | [diff] [blame] | 376 | // Emit final debug information. |
| 377 | DW.EndModule(); |
Evan Cheng | d594881 | 2006-03-07 02:23:26 +0000 | [diff] [blame] | 378 | } |
Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 379 | |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 380 | AsmPrinter::doFinalization(M); |
| 381 | return false; // success |
| 382 | } |
| 383 | |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 384 | /// createX86CodePrinterPass - Returns a pass that prints the X86 assembly code |
| 385 | /// for a MachineFunction to the given output stream, using the given target |
| 386 | /// machine description. |
| 387 | /// |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 388 | FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o, |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 389 | X86TargetMachine &tm) { |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 390 | const X86Subtarget *Subtarget = &tm.getSubtarget<X86Subtarget>(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 391 | |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 392 | if (Subtarget->isFlavorIntel()) { |
Jim Laskey | a0f3d17 | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 393 | return new X86IntelAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
Jim Laskey | 05a059d | 2006-09-07 12:23:47 +0000 | [diff] [blame] | 394 | } else { |
Jim Laskey | a0f3d17 | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 395 | return new X86ATTAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
Chris Lattner | ac5701c | 2004-10-04 07:24:48 +0000 | [diff] [blame] | 396 | } |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 397 | } |