blob: 4c7ccbd8a0a8f0df93e196e586253d9c0a2c9aca [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +00007//
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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
Cedric Venetd85f51a2008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000029#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000031#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000033#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000034#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000036
Chris Lattner95b2c7d2006-12-19 22:59:26 +000037STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Evan Cheng0475ab52008-01-05 00:41:47 +000039static std::string getPICLabelString(unsigned FnNum,
40 const TargetAsmInfo *TAI,
41 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000042 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000043 if (Subtarget->isTargetDarwin())
Evan Cheng347d39f2007-10-14 05:57:21 +000044 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000045 else if (Subtarget->isTargetELF())
Dan Gohmand19a53b2008-06-30 22:03:41 +000046 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000047 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000048 assert(0 && "Don't know how to print PIC label!\n");
49
50 return label;
51}
52
Anton Korobeynikov75b68822008-06-28 11:08:27 +000053static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
54 const TargetData *TD) {
55 X86MachineFunctionInfo Info;
56 uint64_t Size = 0;
57
58 switch (F->getCallingConv()) {
59 case CallingConv::X86_StdCall:
60 Info.setDecorationStyle(StdCall);
61 break;
62 case CallingConv::X86_FastCall:
63 Info.setDecorationStyle(FastCall);
64 break;
65 default:
66 return Info;
67 }
68
69 unsigned argNum = 1;
70 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
71 AI != AE; ++AI, ++argNum) {
72 const Type* Ty = AI->getType();
73
74 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000075 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000076 Ty = cast<PointerType>(Ty)->getElementType();
77
78 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000079 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000080 }
81
82 // We're not supporting tooooo huge arguments :)
83 Info.setBytesToPopOnReturn((unsigned int)Size);
84 return Info;
85}
86
87/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman0f8b53f2009-03-03 02:55:14 +000088/// Don't print things like \\n or \\0.
Owen Andersoncb371882008-08-21 00:14:44 +000089static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +000090 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
91 Name != E; ++Name)
92 if (isprint(*Name))
93 OS << *Name;
94}
95
96/// decorateName - Query FunctionInfoMap and use this information for various
97/// name decoration.
98void X86ATTAsmPrinter::decorateName(std::string &Name,
99 const GlobalValue *GV) {
100 const Function *F = dyn_cast<Function>(GV);
101 if (!F) return;
102
103 // We don't want to decorate non-stdcall or non-fastcall functions right now
104 unsigned CC = F->getCallingConv();
105 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
106 return;
107
108 // Decorate names only when we're targeting Cygwin/Mingw32 targets
109 if (!Subtarget->isTargetCygMing())
110 return;
111
112 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
113
114 const X86MachineFunctionInfo *Info;
115 if (info_item == FunctionInfoMap.end()) {
116 // Calculate apropriate function info and populate map
117 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
118 Info = &FunctionInfoMap[F];
119 } else {
120 Info = &info_item->second;
121 }
122
123 const FunctionType *FT = F->getFunctionType();
124 switch (Info->getDecorationStyle()) {
125 case None:
126 break;
127 case StdCall:
128 // "Pure" variadic functions do not receive @0 suffix.
129 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
131 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
132 break;
133 case FastCall:
134 // "Pure" variadic functions do not receive @0 suffix.
135 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
137 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
138
139 if (Name[0] == '_') {
140 Name[0] = '@';
141 } else {
142 Name = '@' + Name;
143 }
144 break;
145 default:
146 assert(0 && "Unsupported DecorationStyle");
147 }
148}
149
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000150void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000151 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000152
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000153 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000154
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000155 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov91364382008-06-28 11:08:09 +0000156
Devang Patel4ae641f2008-10-01 23:18:38 +0000157 unsigned FnAlign = 4;
Devang Patele4d4b8c2008-10-06 17:30:07 +0000158 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Pateldb100332008-09-04 21:03:41 +0000159 FnAlign = 1;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000160 switch (F->getLinkage()) {
161 default: assert(0 && "Unknown linkage type!");
162 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000163 case Function::PrivateLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000164 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000165 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000166 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000167 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000168 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000169 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000170 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000171 case Function::LinkOnceAnyLinkage:
172 case Function::LinkOnceODRLinkage:
173 case Function::WeakAnyLinkage:
174 case Function::WeakODRLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
178 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000182 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000184 }
185 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000186 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000187
188 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000189
190 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000191 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000192 else if (Subtarget->isTargetCygMing()) {
193 O << "\t.def\t " << CurrentFnName
194 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000195 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000196 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
197 << ";\t.endef\n";
198 }
199
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000200 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000201 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000202 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000203 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000204 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000205}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000206
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000207/// runOnMachineFunction - This uses the printMachineInstruction()
208/// method to print assembly for each instruction.
209///
210bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
211 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000212 this->MF = &MF;
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000213 unsigned CC = F->getCallingConv();
214
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000215 SetupMachineFunction(MF);
216 O << "\n\n";
217
218 // Populate function information map. Actually, We don't want to populate
219 // non-stdcall or non-fastcall functions' information right now.
220 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
221 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
222
223 // Print out constants referenced by the function
224 EmitConstantPool(MF.getConstantPool());
225
226 if (F->hasDLLExportLinkage())
227 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
228
229 // Print the 'header' of function
230 emitFunctionHeader(MF);
231
232 // Emit pre-function debug and/or EH information.
233 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000234 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000235
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000236 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000237 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000238 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
239 I != E; ++I) {
240 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000241 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
242 // This is an entry block or a block that's only reachable via a
243 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
244 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000245 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000246 O << '\n';
247 }
Bill Wendling824a7212008-01-26 09:03:52 +0000248 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
249 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000250 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000251 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000252 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000253 printMachineInstruction(II);
254 }
255 }
Evan Cheng67afece2006-08-28 22:14:16 +0000256
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000257 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
258 // If the function is empty, then we need to emit *something*. Otherwise,
259 // the function's label might be associated with something that it wasn't
260 // meant to be associated with. We emit a noop in this situation.
261 // We are assuming inline asms are code.
262 O << "\tnop\n";
263 }
264
Jim Laskey563321a2006-09-06 18:34:40 +0000265 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000266 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000267
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000268 // Emit post-function debug information.
269 if (TAI->doesSupportDebugInformation())
Devang Pateleb3fc282009-01-08 23:40:34 +0000270 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000271
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000272 // Print out jump tables referenced by the function.
273 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000274
Dan Gohmane5f4de42008-11-07 19:49:17 +0000275 O.flush();
276
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000277 // We didn't modify anything.
278 return false;
279}
280
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000281static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000282 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
283}
284
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000285static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
286 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
287 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
288}
289
290static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000291 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
292}
293
Chris Lattnera3b8c572006-02-06 23:41:19 +0000294void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000295 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000296 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000297 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000298 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000299 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000300 "Virtual registers should not make it this far!");
301 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000302 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000303 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000304 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000305 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
306 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000307 Reg = getX86SubSuperRegister(Reg, VT);
308 }
Evan Chengae270f62008-07-07 22:21:06 +0000309 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000310 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000311 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000312
Chris Lattner63b3d712006-05-04 17:21:20 +0000313 case MachineOperand::MO_Immediate:
Evan Chenga0652002009-03-12 18:15:39 +0000314 if (!Modifier || (strcmp(Modifier, "debug") &&
315 strcmp(Modifier, "mem") &&
316 strcmp(Modifier, "call")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000317 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000318 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000319 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000320 case MachineOperand::MO_MachineBasicBlock:
Evan Chengf1c0ae92009-03-24 00:17:40 +0000321 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000322 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000323 case MachineOperand::MO_JumpTableIndex: {
324 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
325 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000326 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000327 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000328
329 if (TM.getRelocationModel() == Reloc::PIC_) {
330 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000331 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
332 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000333 else if (Subtarget->isPICStyleGOT())
334 O << "@GOTOFF";
335 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000336
Evan Chengae19abc2007-01-18 22:27:12 +0000337 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000338 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000339 return;
340 }
Evan Chenga09bd812006-02-26 08:28:12 +0000341 case MachineOperand::MO_ConstantPoolIndex: {
342 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
343 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000344 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000345 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000346
347 if (TM.getRelocationModel() == Reloc::PIC_) {
348 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000349 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
350 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000351 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000352 O << "@GOTOFF";
353 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000354
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000355 printOffset(MO.getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +0000356
Evan Chengae19abc2007-01-18 22:27:12 +0000357 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000358 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000359 return;
360 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000361 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000362 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000363 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000364 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000365
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000366 const GlobalValue *GV = MO.getGlobal();
367 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
368 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000369 // If GV is an alias then use the aliasee for determining
370 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000371 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000372 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000373 }
374
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000375 bool isThreadLocal = GVar && GVar->isThreadLocal();
376
Evan Cheng25ab6902006-09-08 06:48:29 +0000377 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000378 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000379
Dan Gohman2a3250c2007-04-26 21:07:05 +0000380 if (!isMemOp && !isCallOp)
381 O << '$';
382 else if (Name[0] == '$') {
383 // The name begins with a dollar-sign. In order to avoid having it look
384 // like an integer immediate to the assembler, enclose it in parens.
385 O << '(';
386 needCloseParen = true;
387 }
388
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000389 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000390 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000391 // non-lazily-resolved stubs
Duncan Sands667d4b82009-03-07 15:45:40 +0000392 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000393 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000394 if (isCallOp && isa<Function>(GV)) {
Evan Cheng91a23c82008-09-20 00:13:45 +0000395 // Function stubs are no longer needed for Mac OS X 10.5 and up.
396 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
397 O << Name;
398 } else {
399 FnStubs.insert(Name);
400 printSuffixedName(Name, "$stub");
401 }
Evan Chengae94e592008-12-05 01:06:39 +0000402 } else if (GV->hasHiddenVisibility()) {
403 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
404 // Definition is not definitely in the current translation unit.
405 O << Name;
406 else {
407 HiddenGVStubs.insert(Name);
408 printSuffixedName(Name, "$non_lazy_ptr");
409 }
Evan Cheng2338c5c2006-02-07 08:38:37 +0000410 } else {
411 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000412 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000413 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000414 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000415 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000416 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000418 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000419
Chris Lattner35d86fe2006-07-26 21:12:04 +0000420 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000421 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000422 } else {
423 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000424 O << "__imp_";
425 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000426 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000427
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000428 if (isCallOp) {
429 if (shouldPrintPLT(TM, Subtarget)) {
430 // Assemble call via PLT for externally visible symbols
431 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000432 !GV->hasLocalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000433 O << "@PLT";
434 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000435 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000436 // Save function name for later type emission
437 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000438 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000439 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000440
Evan Cheng6d7d3102006-12-01 07:38:23 +0000441 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000442 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000443
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000444 printOffset(MO.getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +0000445
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000446 if (isThreadLocal) {
Rafael Espindola9a580232009-02-27 13:37:18 +0000447 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
448 switch (model) {
449 case TLSModel::GeneralDynamic:
450 O << "@TLSGD";
451 break;
452 case TLSModel::LocalDynamic:
453 // O << "@TLSLD"; // local dynamic not implemented
454 O << "@TLSGD";
455 break;
456 case TLSModel::InitialExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000457 if (Subtarget->is64Bit()) {
458 assert (!NotRIPRel);
459 O << "@GOTTPOFF(%rip)";
460 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +0000461 O << "@INDNTPOFF";
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000462 }
Rafael Espindola9a580232009-02-27 13:37:18 +0000463 break;
464 case TLSModel::LocalExec:
465 if (Subtarget->is64Bit())
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000466 O << "@TPOFF";
Rafael Espindola9a580232009-02-27 13:37:18 +0000467 else
468 O << "@NTPOFF";
469 break;
470 default:
471 assert (0 && "Unknown TLS model");
472 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000473 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000474 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000475 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000476 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000477 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000478 O << "@GOTOFF";
Dan Gohman72bb0a62009-03-14 02:33:41 +0000479 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel) {
480 if (TM.getRelocationModel() != Reloc::Static) {
481 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
482 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000483
Dan Gohman72bb0a62009-03-14 02:33:41 +0000484 if (needCloseParen) {
485 needCloseParen = false;
486 O << ')';
487 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000488 }
489
Evan Chengae19abc2007-01-18 22:27:12 +0000490 // Use rip when possible to reduce code size, except when
491 // index or base register are also part of the address. e.g.
492 // foo(%rip)(%rcx,%rax,4) is not legal
493 O << "(%rip)";
494 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000495 }
496
Dan Gohman2a3250c2007-04-26 21:07:05 +0000497 if (needCloseParen)
498 O << ')';
499
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000500 return;
501 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000502 case MachineOperand::MO_ExternalSymbol: {
503 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman146a3102009-04-23 00:57:37 +0000504 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000505 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000506 std::string Name(TAI->getGlobalPrefix());
507 Name += MO.getSymbolName();
Evan Cheng91a23c82008-09-20 00:13:45 +0000508 // Print function stub suffix unless it's Mac OS X 10.5 and up.
509 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
510 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000511 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000512 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000513 return;
514 }
Dan Gohman146a3102009-04-23 00:57:37 +0000515 if (!isMemOp && !isCallOp)
Dan Gohman2a3250c2007-04-26 21:07:05 +0000516 O << '$';
517 else if (Name[0] == '$') {
518 // The name begins with a dollar-sign. In order to avoid having it look
519 // like an integer immediate to the assembler, enclose it in parens.
520 O << '(';
521 needCloseParen = true;
522 }
523
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000524 O << Name;
525
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000526 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000527 std::string GOTName(TAI->getGlobalPrefix());
528 GOTName+="_GLOBAL_OFFSET_TABLE_";
529 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000530 // HACK! Emit extra offset to PC during printing GOT offset to
531 // compensate for the size of popl instruction. The resulting code
532 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000533 // call .piclabel
534 // piclabel:
535 // popl %some_register
536 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000537 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000538 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000539
540 if (isCallOp)
541 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000542 }
543
Dan Gohman2a3250c2007-04-26 21:07:05 +0000544 if (needCloseParen)
545 O << ')';
546
Evan Chengae19abc2007-01-18 22:27:12 +0000547 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000548 O << "(%rip)";
549
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000550 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000551 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000552 default:
553 O << "<unknown operand type>"; return;
554 }
555}
556
Nate Begeman391c5d22005-11-30 18:54:35 +0000557void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000558 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000559 assert(value <= 7 && "Invalid ssecc argument!");
560 switch (value) {
561 case 0: O << "eq"; break;
562 case 1: O << "lt"; break;
563 case 2: O << "le"; break;
564 case 3: O << "unord"; break;
565 case 4: O << "neq"; break;
566 case 5: O << "nlt"; break;
567 case 6: O << "nle"; break;
568 case 7: O << "ord"; break;
569 }
570}
571
Rafael Espindola094fad32009-04-08 21:14:34 +0000572void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000573 const char *Modifier,
574 bool NotRIPRel) {
Chris Lattner32c9a452007-01-14 00:13:07 +0000575 MachineOperand BaseReg = MI->getOperand(Op);
576 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000577 const MachineOperand &DispSpec = MI->getOperand(Op+3);
578
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000579 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmand735b802008-10-03 15:45:36 +0000580 if (DispSpec.isGlobal() ||
581 DispSpec.isCPI() ||
Dan Gohman146a3102009-04-23 00:57:37 +0000582 DispSpec.isJTI() ||
583 DispSpec.isSymbol()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000584 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000585 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000586 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000587 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
588 O << DispVal;
589 }
590
591 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000592 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000593 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000594
Chris Lattner32c9a452007-01-14 00:13:07 +0000595 // There are cases where we can end up with ESP/RSP in the indexreg slot.
596 // If this happens, swap the base/index register to support assemblers that
597 // don't work when the index is *SP.
598 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
599 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
600 std::swap(BaseReg, IndexReg);
601 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000602 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000603
Dan Gohmand19a53b2008-06-30 22:03:41 +0000604 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000605 if (BaseReg.getReg())
606 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000607
608 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000609 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000610 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000611 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000612 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000613 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000614 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000615 }
616}
617
Rafael Espindola094fad32009-04-08 21:14:34 +0000618void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000619 const char *Modifier, bool NotRIPRel){
Rafael Espindola094fad32009-04-08 21:14:34 +0000620 assert(isMem(MI, Op) && "Invalid memory reference!");
621 MachineOperand Segment = MI->getOperand(Op+4);
622 if (Segment.getReg()) {
623 printOperand(MI, Op+4, Modifier);
624 O << ':';
625 }
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000626 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindola094fad32009-04-08 21:14:34 +0000627}
628
Anton Korobeynikov91364382008-06-28 11:08:09 +0000629void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000630 const MachineBasicBlock *MBB) const {
631 if (!TAI->getSetDirective())
632 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000633
634 // We don't need .set machinery if we have GOT-style relocations
635 if (Subtarget->isPICStyleGOT())
636 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000637
Evan Chengcc415862007-11-09 01:32:10 +0000638 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
639 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000640 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000641 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000642 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000643 << '_' << uid << '\n';
644 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000645 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000646}
647
Evan Cheng7ccced62006-02-18 00:15:05 +0000648void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000649 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000650 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000651}
652
Evan Cheng62f27002006-04-28 23:11:40 +0000653
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000654void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
655 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000656 unsigned uid) const
657{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000658 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
659 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
660
661 O << JTEntryDirective << ' ';
662
663 if (TM.getRelocationModel() == Reloc::PIC_) {
664 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
665 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
666 << '_' << uid << "_set_" << MBB->getNumber();
667 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000668 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000669 O << "@GOTOFF";
670 } else
671 assert(0 && "Don't know how to print MBB label for this PIC mode");
672 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000673 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000674}
675
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000676bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000677 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000678 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000679 switch (Mode) {
680 default: return true; // Unknown mode.
681 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000682 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000683 break;
684 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000685 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000686 break;
687 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000688 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000689 break;
690 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000691 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000692 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000693 case 'q': // Print DImode register
694 Reg = getX86SubSuperRegister(Reg, MVT::i64);
695 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000696 }
697
Evan Chengae270f62008-07-07 22:21:06 +0000698 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000699 return false;
700}
701
Evan Cheng3d48a902006-04-28 21:19:05 +0000702/// PrintAsmOperand - Print out an operand for an inline asm expression.
703///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000704bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000705 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000706 const char *ExtraCode) {
707 // Does this asm operand have a single letter operand modifier?
708 if (ExtraCode && ExtraCode[0]) {
709 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000710
Evan Cheng3d48a902006-04-28 21:19:05 +0000711 switch (ExtraCode[0]) {
712 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000713 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohman72bb0a62009-03-14 02:33:41 +0000714 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattner0d924992006-10-31 20:12:30 +0000715 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000716 case 'b': // Print QImode register
717 case 'h': // Print QImode high register
718 case 'w': // Print HImode register
719 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000720 case 'q': // Print DImode register
Dan Gohmand735b802008-10-03 15:45:36 +0000721 if (MI->getOperand(OpNo).isReg())
Chris Lattner14393522007-03-25 02:01:03 +0000722 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
723 printOperand(MI, OpNo);
724 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000725
Chris Lattner7cd5e072007-03-25 01:44:57 +0000726 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000727 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000728 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000729 }
730 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000731
Evan Cheng3d48a902006-04-28 21:19:05 +0000732 printOperand(MI, OpNo);
733 return false;
734}
735
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000736bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000737 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000738 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000739 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000740 if (ExtraCode && ExtraCode[0]) {
741 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000742
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000743 switch (ExtraCode[0]) {
744 default: return true; // Unknown modifier.
745 case 'b': // Print QImode register
746 case 'h': // Print QImode high register
747 case 'w': // Print HImode register
748 case 'k': // Print SImode register
749 case 'q': // Print SImode register
750 // These only apply to registers, ignore on mem.
751 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000752 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000753 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattner91387de2009-01-23 22:33:40 +0000754 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000755 }
756 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000757 printMemReference(MI, OpNo);
758 return false;
759}
760
Bill Wendlingac06d002009-02-06 21:45:08 +0000761/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
762/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000763///
764void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
765 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000766
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000767 // Call the autogenerated instruction printer routines.
768 printInstruction(MI);
769}
770
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000771/// doInitialization
772bool X86ATTAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000773
Evan Cheng526be702008-07-09 06:36:53 +0000774 bool Result = AsmPrinter::doInitialization(M);
775
Dale Johannesen7bc39e22008-07-09 20:55:35 +0000776 if (TAI->doesSupportDebugInformation()) {
777 // Let PassManager know we need debug information and relay
778 // the MachineModuleInfo address on to DwarfWriter.
779 // AsmPrinter::doInitialization did this analysis.
Duncan Sands1465d612009-01-28 13:14:17 +0000780 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
781 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000782 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen7bc39e22008-07-09 20:55:35 +0000783 }
784
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000785 // Darwin wants symbols to be quoted if they have complex names.
786 if (Subtarget->isTargetDarwin())
787 Mang->setUseQuotes(true);
788
789 return Result;
790}
791
792
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000793void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000794 const TargetData *TD = TM.getTargetData();
795
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000796 if (!GVar->hasInitializer())
797 return; // External global require no code
798
799 // Check to see if this is a special global used by LLVM, if so, emit it.
800 if (EmitSpecialLLVMGlobal(GVar)) {
801 if (Subtarget->isTargetDarwin() &&
802 TM.getRelocationModel() == Reloc::Static) {
803 if (GVar->getName() == "llvm.global_ctors")
804 O << ".reference .constructors_used\n";
805 else if (GVar->getName() == "llvm.global_dtors")
806 O << ".reference .destructors_used\n";
807 }
808 return;
809 }
810
811 std::string name = Mang->getValueName(GVar);
812 Constant *C = GVar->getInitializer();
813 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000814 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000815 unsigned Align = TD->getPreferredAlignmentLog(GVar);
816
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000817 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000818
819 if (Subtarget->isTargetELF())
820 O << "\t.type\t" << name << ",@object\n";
821
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000822 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000823
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000824 if (C->isNullValue() && !GVar->hasSection() &&
825 !(Subtarget->isTargetDarwin() &&
826 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000827 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000828 if (GVar->hasExternalLinkage()) {
829 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000830 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000831 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000832 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 return;
834 }
835 }
836
837 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000838 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000839 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000840
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000841 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000842 if (GVar->hasLocalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000843 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000844 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000845 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000846 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000847 O << "\t.globl " << name << '\n'
848 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000849 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000850 O << name << ":";
851 if (VerboseAsm) {
Evan Cheng7db860d2009-03-25 01:08:42 +0000852 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +0000853 PrintUnmangledNameSafely(GVar, O);
854 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000855 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 EmitGlobalConstant(C);
857 return;
858 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000859 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16b7f512008-08-08 18:25:52 +0000860 if (TAI->getCOMMDirectiveTakesAlignment())
861 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000862 }
863 } else {
864 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000865 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000866 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000867 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000868 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000869 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000870 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000871 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000872 if (VerboseAsm) {
873 O << "\t\t" << TAI->getCommentString() << ' ';
874 PrintUnmangledNameSafely(GVar, O);
875 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000876 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000877 return;
878 }
879 }
880
881 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000882 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000883 case GlobalValue::LinkOnceAnyLinkage:
884 case GlobalValue::LinkOnceODRLinkage:
885 case GlobalValue::WeakAnyLinkage:
886 case GlobalValue::WeakODRLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000887 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000888 O << "\t.globl " << name << '\n'
889 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000890 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000891 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000892 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000893 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000894 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000895 }
896 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000897 case GlobalValue::DLLExportLinkage:
898 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000899 // FIXME: appending linkage variables should go into a section of
900 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000901 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000902 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000903 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000904 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000905 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000906 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000907 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000908 default:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000909 assert(0 && "Unknown linkage type!");
910 }
911
912 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000913 O << name << ":";
914 if (VerboseAsm){
Evan Cheng7db860d2009-03-25 01:08:42 +0000915 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +0000916 PrintUnmangledNameSafely(GVar, O);
917 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000918 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000919 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000920 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000921
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000922 EmitGlobalConstant(C);
923}
924
Evan Cheng77c8f762008-07-08 00:55:58 +0000925/// printGVStub - Print stub for a global value.
926///
927void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000928 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000929 O << ":\n\t.indirect_symbol ";
930 if (Prefix) O << Prefix;
931 O << GV << "\n\t.long\t0\n";
932}
933
Evan Chengae94e592008-12-05 01:06:39 +0000934/// printHiddenGVStub - Print stub for a hidden global value.
935///
936void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
937 EmitAlignment(2);
938 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
939 if (Prefix) O << Prefix;
940 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
941}
942
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000943
944bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000945 // Print out module-level global variables here.
946 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000947 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000948 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000949
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000950 if (I->hasDLLExportLinkage())
951 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000952
953 // If the global is a extern weak symbol, remember to emit the weak
954 // reference!
Mike Stump6726be62009-05-14 23:22:47 +0000955 // FIXME: This is rather hacky, since we'll emit references to ALL weak
956 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000957 // initializers hidden deep inside constant expressions.
958 if (I->hasExternalWeakLinkage())
959 ExtWeakSymbols.insert(I);
960 }
961
962 for (Module::const_iterator I = M.begin(), E = M.end();
963 I != E; ++I) {
964 // If the global is a extern weak symbol, remember to emit the weak
965 // reference!
Mike Stump11adeed2009-05-14 23:23:37 +0000966 // FIXME: This is rather hacky, since we'll emit references to ALL weak
967 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000968 // initializers hidden deep inside constant expressions.
969 if (I->hasExternalWeakLinkage())
970 ExtWeakSymbols.insert(I);
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000971 }
972
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000973 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000974 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000975 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000976
977 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
978 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000979 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000980 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000981
982 if (!DLLExportedFns.empty()) {
983 SwitchToDataSection(".section .drectve");
984 }
985
986 for (StringSet<>::iterator i = DLLExportedFns.begin(),
987 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000988 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000989 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000990
991 if (Subtarget->isTargetDarwin()) {
992 SwitchToDataSection("");
993
994 // Output stubs for dynamically-linked functions
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000995 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chengae94e592008-12-05 01:06:39 +0000996 i != e; ++i) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000997 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
998 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +0000999 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001000 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +00001001 O << ":\n"
1002 "\t.indirect_symbol " << p << "\n"
1003 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001004 }
1005
Dan Gohmand19a53b2008-06-30 22:03:41 +00001006 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001007
Evan Cheng77c8f762008-07-08 00:55:58 +00001008 // Print global value stubs.
1009 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001010 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1011 // Add the (possibly multiple) personalities to the set of global values.
1012 // Only referenced functions get into the Personalities list.
1013 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001014 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +00001015 E = Personalities.end(); I != E; ++I) {
1016 if (!*I)
1017 continue;
1018 if (!InStubSection) {
1019 SwitchToDataSection(
1020 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1021 InStubSection = true;
1022 }
1023 printGVStub((*I)->getNameStart(), "_");
1024 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001025 }
1026
1027 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +00001028 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001029 SwitchToDataSection(
1030 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1031 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +00001032 i != e; ++i)
1033 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001034
Evan Chengae94e592008-12-05 01:06:39 +00001035 if (!HiddenGVStubs.empty()) {
1036 SwitchToSection(TAI->getDataSection());
1037 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1038 i != e; ++i)
1039 printHiddenGVStub(i->getKeyData());
1040 }
1041
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001042 // Emit final debug information.
Duncan Sands1465d612009-01-28 13:14:17 +00001043 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Pateleb3fc282009-01-08 23:40:34 +00001044 DW->EndModule();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001045
1046 // Funny Darwin hack: This flag tells the linker that no global symbols
1047 // contain code that falls through to other global symbols (e.g. the obvious
1048 // implementation of multiple entry points). If this doesn't occur, the
1049 // linker can safely perform dead code stripping. Since LLVM never
1050 // generates code that does this, it is always safe to set.
1051 O << "\t.subsections_via_symbols\n";
1052 } else if (Subtarget->isTargetCygMing()) {
1053 // Emit type information for external functions
1054 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1055 i != e; ++i) {
1056 O << "\t.def\t " << i->getKeyData()
1057 << ";\t.scl\t" << COFF::C_EXT
1058 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1059 << ";\t.endef\n";
1060 }
1061
1062 // Emit final debug information.
Duncan Sands1465d612009-01-28 13:14:17 +00001063 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Pateleb3fc282009-01-08 23:40:34 +00001064 DW->EndModule();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001065 } else if (Subtarget->isTargetELF()) {
1066 // Emit final debug information.
Duncan Sands1465d612009-01-28 13:14:17 +00001067 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Pateleb3fc282009-01-08 23:40:34 +00001068 DW->EndModule();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001069 }
1070
1071 return AsmPrinter::doFinalization(M);
1072}
1073
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001074// Include the auto-generated portion of the assembly writer.
1075#include "X86GenAsmWriter.inc"