blob: 2c9975eb73b379af1475dea7c9bc692dab00bfd3 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetAsmInfo.h"
33#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
36STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
Evan Cheng0729ccf2008-01-05 00:41:47 +000038static std::string getPICLabelString(unsigned FnNum,
39 const TargetAsmInfo *TAI,
40 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 std::string label;
42 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000043 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000045 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 else
47 assert(0 && "Don't know how to print PIC label!\n");
48
49 return label;
50}
51
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000052static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
53 const TargetData *TD) {
54 X86MachineFunctionInfo Info;
55 uint64_t Size = 0;
56
57 switch (F->getCallingConv()) {
58 case CallingConv::X86_StdCall:
59 Info.setDecorationStyle(StdCall);
60 break;
61 case CallingConv::X86_FastCall:
62 Info.setDecorationStyle(FastCall);
63 break;
64 default:
65 return Info;
66 }
67
68 unsigned argNum = 1;
69 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
70 AI != AE; ++AI, ++argNum) {
71 const Type* Ty = AI->getType();
72
73 // 'Dereference' type in case of byval parameter attribute
74 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
75 Ty = cast<PointerType>(Ty)->getElementType();
76
77 // Size should be aligned to DWORD boundary
78 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
79 }
80
81 // We're not supporting tooooo huge arguments :)
82 Info.setBytesToPopOnReturn((unsigned int)Size);
83 return Info;
84}
85
86/// PrintUnmangledNameSafely - Print out the printable characters in the name.
87/// Don't print things like \n or \0.
Owen Anderson847b99b2008-08-21 00:14:44 +000088static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000089 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
90 Name != E; ++Name)
91 if (isprint(*Name))
92 OS << *Name;
93}
94
95/// decorateName - Query FunctionInfoMap and use this information for various
96/// name decoration.
97void X86ATTAsmPrinter::decorateName(std::string &Name,
98 const GlobalValue *GV) {
99 const Function *F = dyn_cast<Function>(GV);
100 if (!F) return;
101
102 // We don't want to decorate non-stdcall or non-fastcall functions right now
103 unsigned CC = F->getCallingConv();
104 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
105 return;
106
107 // Decorate names only when we're targeting Cygwin/Mingw32 targets
108 if (!Subtarget->isTargetCygMing())
109 return;
110
111 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
112
113 const X86MachineFunctionInfo *Info;
114 if (info_item == FunctionInfoMap.end()) {
115 // Calculate apropriate function info and populate map
116 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
117 Info = &FunctionInfoMap[F];
118 } else {
119 Info = &info_item->second;
120 }
121
122 const FunctionType *FT = F->getFunctionType();
123 switch (Info->getDecorationStyle()) {
124 case None:
125 break;
126 case StdCall:
127 // "Pure" variadic functions do not receive @0 suffix.
128 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
129 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
130 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
131 break;
132 case FastCall:
133 // "Pure" variadic functions do not receive @0 suffix.
134 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
135 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
137
138 if (Name[0] == '_') {
139 Name[0] = '@';
140 } else {
141 Name = '@' + Name;
142 }
143 break;
144 default:
145 assert(0 && "Unsupported DecorationStyle");
146 }
147}
148
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000149// Substitute old hook with new one temporary
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000151 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152}
153
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000154void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 const Function *F = MF.getFunction();
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000156 std::string SectionName = TAI->SectionForGlobal(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000158 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000160 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000161
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000162 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Devang Patel5d3ea162008-09-23 23:52:03 +0000163 if (!F->isDeclaration() && F->hasNote(FN_NOTE_OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000164 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 switch (F->getLinkage()) {
166 default: assert(0 && "Unknown linkage type!");
167 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000168 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 break;
170 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000173 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 break;
175 case Function::LinkOnceLinkage:
176 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000177 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000179 O << "\t.globl\t" << CurrentFnName << '\n';
180 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000182 O << "\t.globl\t" << CurrentFnName << "\n"
183 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000185 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 }
187 break;
188 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000189
190 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
192 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000193 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 else if (Subtarget->isTargetCygMing()) {
195 O << "\t.def\t " << CurrentFnName
196 << ";\t.scl\t" <<
197 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
198 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
199 << ";\t.endef\n";
200 }
201
202 O << CurrentFnName << ":\n";
203 // Add some workaround for linkonce linkage on Cygwin\MinGW
204 if (Subtarget->isTargetCygMing() &&
205 (F->getLinkage() == Function::LinkOnceLinkage ||
206 F->getLinkage() == Function::WeakLinkage))
207 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000208}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000210/// runOnMachineFunction - This uses the printMachineInstruction()
211/// method to print assembly for each instruction.
212///
213bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
214 const Function *F = MF.getFunction();
215 unsigned CC = F->getCallingConv();
216
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000217 SetupMachineFunction(MF);
218 O << "\n\n";
219
220 // Populate function information map. Actually, We don't want to populate
221 // non-stdcall or non-fastcall functions' information right now.
222 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
223 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
224
225 // Print out constants referenced by the function
226 EmitConstantPool(MF.getConstantPool());
227
228 if (F->hasDLLExportLinkage())
229 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
230
231 // Print the 'header' of function
232 emitFunctionHeader(MF);
233
234 // Emit pre-function debug and/or EH information.
235 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
236 DW.BeginFunction(&MF);
237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000239 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
241 I != E; ++I) {
242 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000243 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000244 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 O << '\n';
246 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000247 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
248 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000250 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000251 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 printMachineInstruction(II);
253 }
254 }
255
Dale Johannesenf35771f2008-04-08 00:37:56 +0000256 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
257 // If the function is empty, then we need to emit *something*. Otherwise,
258 // the function's label might be associated with something that it wasn't
259 // meant to be associated with. We emit a noop in this situation.
260 // We are assuming inline asms are code.
261 O << "\tnop\n";
262 }
263
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000265 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000267 // Emit post-function debug information.
268 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270
271 // Print out jump tables referenced by the function.
272 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 // We didn't modify anything.
275 return false;
276}
277
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000278static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
280}
281
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000282static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
283 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
284 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
285}
286
287static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
289}
290
291void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
292 const char *Modifier, bool NotRIPRel) {
293 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 switch (MO.getType()) {
295 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000296 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 "Virtual registers should not make it this far!");
298 O << '%';
299 unsigned Reg = MO.getReg();
300 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000301 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
303 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
304 Reg = getX86SubSuperRegister(Reg, VT);
305 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000306 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 return;
308 }
309
310 case MachineOperand::MO_Immediate:
311 if (!Modifier ||
312 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
313 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000314 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 return;
316 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000317 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 return;
319 case MachineOperand::MO_JumpTableIndex: {
320 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
321 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000322 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000323 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 if (TM.getRelocationModel() == Reloc::PIC_) {
326 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000327 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
328 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 else if (Subtarget->isPICStyleGOT())
330 O << "@GOTOFF";
331 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
334 O << "(%rip)";
335 return;
336 }
337 case MachineOperand::MO_ConstantPoolIndex: {
338 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
339 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000340 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000341 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342
343 if (TM.getRelocationModel() == Reloc::PIC_) {
344 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000345 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
346 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 else if (Subtarget->isPICStyleGOT())
348 O << "@GOTOFF";
349 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000350
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 int Offset = MO.getOffset();
352 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000353 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 else if (Offset < 0)
355 O << Offset;
356
357 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
358 O << "(%rip)";
359 return;
360 }
361 case MachineOperand::MO_GlobalAddress: {
362 bool isCallOp = Modifier && !strcmp(Modifier, "call");
363 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
364 bool needCloseParen = false;
365
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000366 const GlobalValue *GV = MO.getGlobal();
367 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
368 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000369 // If GV is an alias then use the aliasee for determining
370 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000371 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000372 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000373 }
374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 bool isThreadLocal = GVar && GVar->isThreadLocal();
376
377 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000378 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000379
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Espindolae0ac18d2008-06-09 09:52:31 +0000389 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 // Link-once, declaration, or Weakly-linked global variables need
391 // non-lazily-resolved stubs
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000392 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 // Dynamically-resolved functions need a stub for the function.
394 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 } else {
403 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000404 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 }
406 } else {
407 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000408 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 O << Name;
410 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000411
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000413 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 } else {
415 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000416 O << "__imp_";
417 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 O << Name;
419
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000420 if (isCallOp) {
421 if (shouldPrintPLT(TM, Subtarget)) {
422 // Assemble call via PLT for externally visible symbols
423 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
424 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 O << "@PLT";
426 }
427 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
428 // Save function name for later type emission
429 FnStubs.insert(Name);
430 }
431 }
432
433 if (GV->hasExternalWeakLinkage())
434 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 int Offset = MO.getOffset();
437 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000438 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 else if (Offset < 0)
440 O << Offset;
441
442 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000443 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 O << "@TLSGD"; // general dynamic TLS model
445 else
446 if (GV->isDeclaration())
447 O << "@INDNTPOFF"; // initial exec TLS model
448 else
449 O << "@NTPOFF"; // local exec TLS model
450 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000451 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
453 O << "@GOT";
454 else
455 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000456 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
457 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000458 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 O << "@GOTPCREL";
460
461 if (needCloseParen) {
462 needCloseParen = false;
463 O << ')';
464 }
465
466 // Use rip when possible to reduce code size, except when
467 // index or base register are also part of the address. e.g.
468 // foo(%rip)(%rcx,%rax,4) is not legal
469 O << "(%rip)";
470 }
471 }
472
473 if (needCloseParen)
474 O << ')';
475
476 return;
477 }
478 case MachineOperand::MO_ExternalSymbol: {
479 bool isCallOp = Modifier && !strcmp(Modifier, "call");
480 bool needCloseParen = false;
481 std::string Name(TAI->getGlobalPrefix());
482 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000483 // Print function stub suffix unless it's Mac OS X 10.5 and up.
484 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
485 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000487 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 return;
489 }
490 if (!isCallOp)
491 O << '$';
492 else if (Name[0] == '$') {
493 // The name begins with a dollar-sign. In order to avoid having it look
494 // like an integer immediate to the assembler, enclose it in parens.
495 O << '(';
496 needCloseParen = true;
497 }
498
499 O << Name;
500
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000501 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 std::string GOTName(TAI->getGlobalPrefix());
503 GOTName+="_GLOBAL_OFFSET_TABLE_";
504 if (Name == GOTName)
505 // HACK! Emit extra offset to PC during printing GOT offset to
506 // compensate for the size of popl instruction. The resulting code
507 // should look like:
508 // call .piclabel
509 // piclabel:
510 // popl %some_register
511 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
512 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000513 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514
515 if (isCallOp)
516 O << "@PLT";
517 }
518
519 if (needCloseParen)
520 O << ')';
521
522 if (!isCallOp && Subtarget->isPICStyleRIPRel())
523 O << "(%rip)";
524
525 return;
526 }
527 default:
528 O << "<unknown operand type>"; return;
529 }
530}
531
532void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000533 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 assert(value <= 7 && "Invalid ssecc argument!");
535 switch (value) {
536 case 0: O << "eq"; break;
537 case 1: O << "lt"; break;
538 case 2: O << "le"; break;
539 case 3: O << "unord"; break;
540 case 4: O << "neq"; break;
541 case 5: O << "nlt"; break;
542 case 6: O << "nle"; break;
543 case 7: O << "ord"; break;
544 }
545}
546
547void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
548 const char *Modifier){
549 assert(isMem(MI, Op) && "Invalid memory reference!");
550 MachineOperand BaseReg = MI->getOperand(Op);
551 MachineOperand IndexReg = MI->getOperand(Op+2);
552 const MachineOperand &DispSpec = MI->getOperand(Op+3);
553
554 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
555 if (DispSpec.isGlobalAddress() ||
556 DispSpec.isConstantPoolIndex() ||
557 DispSpec.isJumpTableIndex()) {
558 printOperand(MI, Op+3, "mem", NotRIPRel);
559 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000560 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
562 O << DispVal;
563 }
564
565 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000566 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000568
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 // There are cases where we can end up with ESP/RSP in the indexreg slot.
570 // If this happens, swap the base/index register to support assemblers that
571 // don't work when the index is *SP.
572 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
573 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
574 std::swap(BaseReg, IndexReg);
575 std::swap(BaseRegOperand, IndexRegOperand);
576 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000577
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000578 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 if (BaseReg.getReg())
580 printOperand(MI, Op+BaseRegOperand, Modifier);
581
582 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000583 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 printOperand(MI, Op+IndexRegOperand, Modifier);
585 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000586 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000588 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 }
590}
591
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000592void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000593 const MachineBasicBlock *MBB) const {
594 if (!TAI->getSetDirective())
595 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000596
597 // We don't need .set machinery if we have GOT-style relocations
598 if (Subtarget->isPICStyleGOT())
599 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000600
Evan Cheng6fb06762007-11-09 01:32:10 +0000601 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
602 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000603 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000604 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000605 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000606 << '_' << uid << '\n';
607 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000608 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000609}
610
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000612 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000613 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614}
615
616
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000617void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
618 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000619 unsigned uid) const
620{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000621 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
622 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
623
624 O << JTEntryDirective << ' ';
625
626 if (TM.getRelocationModel() == Reloc::PIC_) {
627 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
628 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
629 << '_' << uid << "_set_" << MBB->getNumber();
630 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000631 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000632 O << "@GOTOFF";
633 } else
634 assert(0 && "Don't know how to print MBB label for this PIC mode");
635 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000636 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000637}
638
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000639bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 unsigned Reg = MO.getReg();
642 switch (Mode) {
643 default: return true; // Unknown mode.
644 case 'b': // Print QImode register
645 Reg = getX86SubSuperRegister(Reg, MVT::i8);
646 break;
647 case 'h': // Print QImode high register
648 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
649 break;
650 case 'w': // Print HImode register
651 Reg = getX86SubSuperRegister(Reg, MVT::i16);
652 break;
653 case 'k': // Print SImode register
654 Reg = getX86SubSuperRegister(Reg, MVT::i32);
655 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000656 case 'q': // Print DImode register
657 Reg = getX86SubSuperRegister(Reg, MVT::i64);
658 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 }
660
Evan Cheng00d04a72008-07-07 22:21:06 +0000661 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 return false;
663}
664
665/// PrintAsmOperand - Print out an operand for an inline asm expression.
666///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000667bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000668 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 const char *ExtraCode) {
670 // Does this asm operand have a single letter operand modifier?
671 if (ExtraCode && ExtraCode[0]) {
672 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000673
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 switch (ExtraCode[0]) {
675 default: return true; // Unknown modifier.
676 case 'c': // Don't print "$" before a global var name or constant.
677 printOperand(MI, OpNo, "mem");
678 return false;
679 case 'b': // Print QImode register
680 case 'h': // Print QImode high register
681 case 'w': // Print HImode register
682 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000683 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000684 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
686 printOperand(MI, OpNo);
687 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000688
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 case 'P': // Don't print @PLT, but do print as memory.
690 printOperand(MI, OpNo, "mem");
691 return false;
692 }
693 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000694
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 printOperand(MI, OpNo);
696 return false;
697}
698
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000699bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000701 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000703 if (ExtraCode && ExtraCode[0]) {
704 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000705
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000706 switch (ExtraCode[0]) {
707 default: return true; // Unknown modifier.
708 case 'b': // Print QImode register
709 case 'h': // Print QImode high register
710 case 'w': // Print HImode register
711 case 'k': // Print SImode register
712 case 'q': // Print SImode register
713 // These only apply to registers, ignore on mem.
714 break;
715 }
716 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 printMemReference(MI, OpNo);
718 return false;
719}
720
721/// printMachineInstruction -- Print out a single X86 LLVM instruction
722/// MI in AT&T syntax to the current output stream.
723///
724void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
725 ++EmittedInsts;
726
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 // Call the autogenerated instruction printer routines.
728 printInstruction(MI);
729}
730
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000731/// doInitialization
732bool X86ATTAsmPrinter::doInitialization(Module &M) {
733 if (TAI->doesSupportDebugInformation()) {
734 // Emit initial debug information.
735 DW.BeginModule(&M);
736 }
737
Evan Cheng5cda7762008-07-09 06:36:53 +0000738 bool Result = AsmPrinter::doInitialization(M);
739
Dale Johannesen06545922008-07-09 20:55:35 +0000740 if (TAI->doesSupportDebugInformation()) {
741 // Let PassManager know we need debug information and relay
742 // the MachineModuleInfo address on to DwarfWriter.
743 // AsmPrinter::doInitialization did this analysis.
744 MMI = getAnalysisToUpdate<MachineModuleInfo>();
745 DW.SetModuleInfo(MMI);
746 }
747
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000748 // Darwin wants symbols to be quoted if they have complex names.
749 if (Subtarget->isTargetDarwin())
750 Mang->setUseQuotes(true);
751
752 return Result;
753}
754
755
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000756void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000757 const TargetData *TD = TM.getTargetData();
758
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000759 if (!GVar->hasInitializer())
760 return; // External global require no code
761
762 // Check to see if this is a special global used by LLVM, if so, emit it.
763 if (EmitSpecialLLVMGlobal(GVar)) {
764 if (Subtarget->isTargetDarwin() &&
765 TM.getRelocationModel() == Reloc::Static) {
766 if (GVar->getName() == "llvm.global_ctors")
767 O << ".reference .constructors_used\n";
768 else if (GVar->getName() == "llvm.global_dtors")
769 O << ".reference .destructors_used\n";
770 }
771 return;
772 }
773
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000774 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000775 std::string name = Mang->getValueName(GVar);
776 Constant *C = GVar->getInitializer();
777 const Type *Type = C->getType();
778 unsigned Size = TD->getABITypeSize(Type);
779 unsigned Align = TD->getPreferredAlignmentLog(GVar);
780
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000781 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000782
783 if (Subtarget->isTargetELF())
784 O << "\t.type\t" << name << ",@object\n";
785
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000786 SwitchToDataSection(SectionName.c_str());
787
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000788 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000789 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000790 if (GVar->hasExternalLinkage()) {
791 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000792 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000793 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000794 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 return;
796 }
797 }
798
799 if (!GVar->isThreadLocal() &&
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000800 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000801 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000802
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000803 if (TAI->getLCOMMDirective() != NULL) {
804 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000805 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << "\t.globl " << name << '\n'
810 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000811 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 EmitGlobalConstant(C);
816 return;
817 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000818 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000819 if (TAI->getCOMMDirectiveTakesAlignment())
820 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 }
822 } else {
823 if (!Subtarget->isTargetCygMing()) {
824 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000825 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000827 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000828 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000829 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000831 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 return;
835 }
836 }
837
838 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000839 case GlobalValue::CommonLinkage:
840 case GlobalValue::LinkOnceLinkage:
841 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000842 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000843 O << "\t.globl " << name << '\n'
844 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000847 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000849 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 }
851 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000852 case GlobalValue::DLLExportLinkage:
853 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 // FIXME: appending linkage variables should go into a section of
855 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000856 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000859 // FALL THROUGH
Evan Cheng630c5612008-08-08 06:43:59 +0000860 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000862 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 assert(0 && "Unknown linkage type!");
864 }
865
866 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000867 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000868 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000869 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000870 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000871 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872
873 // If the initializer is a extern weak symbol, remember to emit the weak
874 // reference!
875 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
876 if (GV->hasExternalWeakLinkage())
877 ExtWeakSymbols.insert(GV);
878
879 EmitGlobalConstant(C);
880}
881
Evan Cheng76443dc2008-07-08 00:55:58 +0000882/// printGVStub - Print stub for a global value.
883///
884void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000885 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000886 O << ":\n\t.indirect_symbol ";
887 if (Prefix) O << Prefix;
888 O << GV << "\n\t.long\t0\n";
889}
890
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000891
892bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000893 // Print out module-level global variables here.
894 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000895 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000896 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000897
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000898 if (I->hasDLLExportLinkage())
899 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
900 }
901
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000902 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000903 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000904 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000905
906 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
907 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000908 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000910
911 if (!DLLExportedFns.empty()) {
912 SwitchToDataSection(".section .drectve");
913 }
914
915 for (StringSet<>::iterator i = DLLExportedFns.begin(),
916 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000917 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000918 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000919
920 if (Subtarget->isTargetDarwin()) {
921 SwitchToDataSection("");
922
923 // Output stubs for dynamically-linked functions
924 unsigned j = 1;
925 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
926 i != e; ++i, ++j) {
927 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
928 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000929 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000930 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000931 O << ":\n"
932 "\t.indirect_symbol " << p << "\n"
933 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000934 }
935
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000936 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000937
Evan Cheng76443dc2008-07-08 00:55:58 +0000938 // Print global value stubs.
939 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000940 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
941 // Add the (possibly multiple) personalities to the set of global values.
942 // Only referenced functions get into the Personalities list.
943 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000944 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000945 E = Personalities.end(); I != E; ++I) {
946 if (!*I)
947 continue;
948 if (!InStubSection) {
949 SwitchToDataSection(
950 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
951 InStubSection = true;
952 }
953 printGVStub((*I)->getNameStart(), "_");
954 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000955 }
956
957 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000958 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000959 SwitchToDataSection(
960 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
961 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000962 i != e; ++i)
963 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000964
965 // Emit final debug information.
966 DW.EndModule();
967
968 // Funny Darwin hack: This flag tells the linker that no global symbols
969 // contain code that falls through to other global symbols (e.g. the obvious
970 // implementation of multiple entry points). If this doesn't occur, the
971 // linker can safely perform dead code stripping. Since LLVM never
972 // generates code that does this, it is always safe to set.
973 O << "\t.subsections_via_symbols\n";
974 } else if (Subtarget->isTargetCygMing()) {
975 // Emit type information for external functions
976 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
977 i != e; ++i) {
978 O << "\t.def\t " << i->getKeyData()
979 << ";\t.scl\t" << COFF::C_EXT
980 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
981 << ";\t.endef\n";
982 }
983
984 // Emit final debug information.
985 DW.EndModule();
986 } else if (Subtarget->isTargetELF()) {
987 // Emit final debug information.
988 DW.EndModule();
989 }
990
991 return AsmPrinter::doFinalization(M);
992}
993
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994// Include the auto-generated portion of the assembly writer.
995#include "X86GenAsmWriter.inc"