blob: df0b107f9d84b4446cc6ccf1f338098da9a1722c [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
Devang Pateld222f862008-09-25 21:00:45 +000074 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000075 Ty = cast<PointerType>(Ty)->getElementType();
76
77 // Size should be aligned to DWORD boundary
Duncan Sandsd68f13b2009-01-12 20:38:59 +000078 Size += ((TD->getTypePaddedSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000079 }
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 Korobeynikov30948e32008-06-28 11:09:01 +0000149void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000152 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000154 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000155
Devang Patel93698d92008-10-01 23:18:38 +0000156 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000157 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000158 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 switch (F->getLinkage()) {
160 default: assert(0 && "Unknown linkage type!");
161 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000162 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000163 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 break;
165 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000167 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000168 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 break;
170 case Function::LinkOnceLinkage:
171 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000174 O << "\t.globl\t" << CurrentFnName << '\n';
175 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << "\n"
178 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182 break;
183 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000184
185 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000188 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 else if (Subtarget->isTargetCygMing()) {
190 O << "\t.def\t " << CurrentFnName
191 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000192 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
194 << ";\t.endef\n";
195 }
196
197 O << CurrentFnName << ":\n";
198 // Add some workaround for linkonce linkage on Cygwin\MinGW
199 if (Subtarget->isTargetCygMing() &&
200 (F->getLinkage() == Function::LinkOnceLinkage ||
201 F->getLinkage() == Function::WeakLinkage))
202 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000203}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000205/// runOnMachineFunction - This uses the printMachineInstruction()
206/// method to print assembly for each instruction.
207///
208bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
209 const Function *F = MF.getFunction();
210 unsigned CC = F->getCallingConv();
211
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212 SetupMachineFunction(MF);
213 O << "\n\n";
214
215 // Populate function information map. Actually, We don't want to populate
216 // non-stdcall or non-fastcall functions' information right now.
217 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
218 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
219
220 // Print out constants referenced by the function
221 EmitConstantPool(MF.getConstantPool());
222
223 if (F->hasDLLExportLinkage())
224 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
225
226 // Print the 'header' of function
227 emitFunctionHeader(MF);
228
229 // Emit pre-function debug and/or EH information.
230 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000231 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000234 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
236 I != E; ++I) {
237 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000238 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000239 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 O << '\n';
241 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000242 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
243 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000245 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000246 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 printMachineInstruction(II);
248 }
249 }
250
Dale Johannesenf35771f2008-04-08 00:37:56 +0000251 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
252 // If the function is empty, then we need to emit *something*. Otherwise,
253 // the function's label might be associated with something that it wasn't
254 // meant to be associated with. We emit a noop in this situation.
255 // We are assuming inline asms are code.
256 O << "\tnop\n";
257 }
258
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000260 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000262 // Emit post-function debug information.
263 if (TAI->doesSupportDebugInformation())
Devang Patelaa1e8432009-01-08 23:40:34 +0000264 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265
266 // Print out jump tables referenced by the function.
267 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000268
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000269 O.flush();
270
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 // We didn't modify anything.
272 return false;
273}
274
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000275static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
277}
278
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000279static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
280 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
281 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
282}
283
284static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
286}
287
288void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
289 const char *Modifier, bool NotRIPRel) {
290 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 switch (MO.getType()) {
292 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000293 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 "Virtual registers should not make it this far!");
295 O << '%';
296 unsigned Reg = MO.getReg();
297 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000298 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
300 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
301 Reg = getX86SubSuperRegister(Reg, VT);
302 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000303 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 return;
305 }
306
307 case MachineOperand::MO_Immediate:
308 if (!Modifier ||
309 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
310 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000311 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 return;
313 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000314 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 return;
316 case MachineOperand::MO_JumpTableIndex: {
317 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
318 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000319 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000320 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
322 if (TM.getRelocationModel() == Reloc::PIC_) {
323 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000324 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
325 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 else if (Subtarget->isPICStyleGOT())
327 O << "@GOTOFF";
328 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000329
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
331 O << "(%rip)";
332 return;
333 }
334 case MachineOperand::MO_ConstantPoolIndex: {
335 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
336 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000337 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000338 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
340 if (TM.getRelocationModel() == Reloc::PIC_) {
341 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000342 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
343 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 else if (Subtarget->isPICStyleGOT())
345 O << "@GOTOFF";
346 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000347
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000348 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349
350 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
351 O << "(%rip)";
352 return;
353 }
354 case MachineOperand::MO_GlobalAddress: {
355 bool isCallOp = Modifier && !strcmp(Modifier, "call");
356 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
357 bool needCloseParen = false;
358
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000359 const GlobalValue *GV = MO.getGlobal();
360 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
361 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000362 // If GV is an alias then use the aliasee for determining
363 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000364 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000365 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000366 }
367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 bool isThreadLocal = GVar && GVar->isThreadLocal();
369
370 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000371 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000372
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 if (!isMemOp && !isCallOp)
374 O << '$';
375 else if (Name[0] == '$') {
376 // The name begins with a dollar-sign. In order to avoid having it look
377 // like an integer immediate to the assembler, enclose it in parens.
378 O << '(';
379 needCloseParen = true;
380 }
381
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000382 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 // Link-once, declaration, or Weakly-linked global variables need
384 // non-lazily-resolved stubs
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000385 if (GV->isDeclaration() || GV->mayBeOverridden()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 // Dynamically-resolved functions need a stub for the function.
387 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000388 // Function stubs are no longer needed for Mac OS X 10.5 and up.
389 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
390 O << Name;
391 } else {
392 FnStubs.insert(Name);
393 printSuffixedName(Name, "$stub");
394 }
Evan Chenga65854f2008-12-05 01:06:39 +0000395 } else if (GV->hasHiddenVisibility()) {
396 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
397 // Definition is not definitely in the current translation unit.
398 O << Name;
399 else {
400 HiddenGVStubs.insert(Name);
401 printSuffixedName(Name, "$non_lazy_ptr");
402 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 } else {
404 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000405 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 }
407 } else {
408 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000409 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 O << Name;
411 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000412
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000414 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 } else {
416 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000417 O << "__imp_";
418 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 O << Name;
420
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000421 if (isCallOp) {
422 if (shouldPrintPLT(TM, Subtarget)) {
423 // Assemble call via PLT for externally visible symbols
424 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000425 !GV->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 O << "@PLT";
427 }
428 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
429 // Save function name for later type emission
430 FnStubs.insert(Name);
431 }
432 }
433
434 if (GV->hasExternalWeakLinkage())
435 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000436
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000437 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438
439 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000440 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 O << "@TLSGD"; // general dynamic TLS model
442 else
443 if (GV->isDeclaration())
444 O << "@INDNTPOFF"; // initial exec TLS model
445 else
446 O << "@NTPOFF"; // local exec TLS model
447 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000448 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
450 O << "@GOT";
451 else
452 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000453 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
454 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000455 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 O << "@GOTPCREL";
457
458 if (needCloseParen) {
459 needCloseParen = false;
460 O << ')';
461 }
462
463 // Use rip when possible to reduce code size, except when
464 // index or base register are also part of the address. e.g.
465 // foo(%rip)(%rcx,%rax,4) is not legal
466 O << "(%rip)";
467 }
468 }
469
470 if (needCloseParen)
471 O << ')';
472
473 return;
474 }
475 case MachineOperand::MO_ExternalSymbol: {
476 bool isCallOp = Modifier && !strcmp(Modifier, "call");
477 bool needCloseParen = false;
478 std::string Name(TAI->getGlobalPrefix());
479 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000480 // Print function stub suffix unless it's Mac OS X 10.5 and up.
481 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
482 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000484 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 return;
486 }
487 if (!isCallOp)
488 O << '$';
489 else if (Name[0] == '$') {
490 // The name begins with a dollar-sign. In order to avoid having it look
491 // like an integer immediate to the assembler, enclose it in parens.
492 O << '(';
493 needCloseParen = true;
494 }
495
496 O << Name;
497
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000498 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 std::string GOTName(TAI->getGlobalPrefix());
500 GOTName+="_GLOBAL_OFFSET_TABLE_";
501 if (Name == GOTName)
502 // HACK! Emit extra offset to PC during printing GOT offset to
503 // compensate for the size of popl instruction. The resulting code
504 // should look like:
505 // call .piclabel
506 // piclabel:
507 // popl %some_register
508 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
509 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000510 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511
512 if (isCallOp)
513 O << "@PLT";
514 }
515
516 if (needCloseParen)
517 O << ')';
518
519 if (!isCallOp && Subtarget->isPICStyleRIPRel())
520 O << "(%rip)";
521
522 return;
523 }
524 default:
525 O << "<unknown operand type>"; return;
526 }
527}
528
529void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000530 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 assert(value <= 7 && "Invalid ssecc argument!");
532 switch (value) {
533 case 0: O << "eq"; break;
534 case 1: O << "lt"; break;
535 case 2: O << "le"; break;
536 case 3: O << "unord"; break;
537 case 4: O << "neq"; break;
538 case 5: O << "nlt"; break;
539 case 6: O << "nle"; break;
540 case 7: O << "ord"; break;
541 }
542}
543
544void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
545 const char *Modifier){
546 assert(isMem(MI, Op) && "Invalid memory reference!");
547 MachineOperand BaseReg = MI->getOperand(Op);
548 MachineOperand IndexReg = MI->getOperand(Op+2);
549 const MachineOperand &DispSpec = MI->getOperand(Op+3);
550
551 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000552 if (DispSpec.isGlobal() ||
553 DispSpec.isCPI() ||
554 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 printOperand(MI, Op+3, "mem", NotRIPRel);
556 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000557 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
559 O << DispVal;
560 }
561
562 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000563 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000565
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 // There are cases where we can end up with ESP/RSP in the indexreg slot.
567 // If this happens, swap the base/index register to support assemblers that
568 // don't work when the index is *SP.
569 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
570 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
571 std::swap(BaseReg, IndexReg);
572 std::swap(BaseRegOperand, IndexRegOperand);
573 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000574
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000575 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 if (BaseReg.getReg())
577 printOperand(MI, Op+BaseRegOperand, Modifier);
578
579 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000580 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 printOperand(MI, Op+IndexRegOperand, Modifier);
582 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000583 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000585 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 }
587}
588
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000589void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000590 const MachineBasicBlock *MBB) const {
591 if (!TAI->getSetDirective())
592 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000593
594 // We don't need .set machinery if we have GOT-style relocations
595 if (Subtarget->isPICStyleGOT())
596 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000597
Evan Cheng6fb06762007-11-09 01:32:10 +0000598 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
599 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000600 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000601 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000602 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000603 << '_' << uid << '\n';
604 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000605 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000606}
607
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000609 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000610 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611}
612
613
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000614void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
615 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000616 unsigned uid) const
617{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000618 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
619 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
620
621 O << JTEntryDirective << ' ';
622
623 if (TM.getRelocationModel() == Reloc::PIC_) {
624 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
625 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
626 << '_' << uid << "_set_" << MBB->getNumber();
627 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000628 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000629 O << "@GOTOFF";
630 } else
631 assert(0 && "Don't know how to print MBB label for this PIC mode");
632 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000633 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000634}
635
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000636bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 unsigned Reg = MO.getReg();
639 switch (Mode) {
640 default: return true; // Unknown mode.
641 case 'b': // Print QImode register
642 Reg = getX86SubSuperRegister(Reg, MVT::i8);
643 break;
644 case 'h': // Print QImode high register
645 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
646 break;
647 case 'w': // Print HImode register
648 Reg = getX86SubSuperRegister(Reg, MVT::i16);
649 break;
650 case 'k': // Print SImode register
651 Reg = getX86SubSuperRegister(Reg, MVT::i32);
652 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000653 case 'q': // Print DImode register
654 Reg = getX86SubSuperRegister(Reg, MVT::i64);
655 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 }
657
Evan Cheng00d04a72008-07-07 22:21:06 +0000658 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 return false;
660}
661
662/// PrintAsmOperand - Print out an operand for an inline asm expression.
663///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000664bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000665 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 const char *ExtraCode) {
667 // Does this asm operand have a single letter operand modifier?
668 if (ExtraCode && ExtraCode[0]) {
669 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000670
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 switch (ExtraCode[0]) {
672 default: return true; // Unknown modifier.
673 case 'c': // Don't print "$" before a global var name or constant.
674 printOperand(MI, OpNo, "mem");
675 return false;
676 case 'b': // Print QImode register
677 case 'h': // Print QImode high register
678 case 'w': // Print HImode register
679 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000680 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000681 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
683 printOperand(MI, OpNo);
684 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000685
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 case 'P': // Don't print @PLT, but do print as memory.
687 printOperand(MI, OpNo, "mem");
688 return false;
689 }
690 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 printOperand(MI, OpNo);
693 return false;
694}
695
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000696bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000698 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000700 if (ExtraCode && ExtraCode[0]) {
701 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000702
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000703 switch (ExtraCode[0]) {
704 default: return true; // Unknown modifier.
705 case 'b': // Print QImode register
706 case 'h': // Print QImode high register
707 case 'w': // Print HImode register
708 case 'k': // Print SImode register
709 case 'q': // Print SImode register
710 // These only apply to registers, ignore on mem.
711 break;
712 }
713 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 printMemReference(MI, OpNo);
715 return false;
716}
717
718/// printMachineInstruction -- Print out a single X86 LLVM instruction
719/// MI in AT&T syntax to the current output stream.
720///
721void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
722 ++EmittedInsts;
723
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 // Call the autogenerated instruction printer routines.
725 printInstruction(MI);
726}
727
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000728/// doInitialization
729bool X86ATTAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000730
Evan Cheng5cda7762008-07-09 06:36:53 +0000731 bool Result = AsmPrinter::doInitialization(M);
732
Dale Johannesen06545922008-07-09 20:55:35 +0000733 if (TAI->doesSupportDebugInformation()) {
734 // Let PassManager know we need debug information and relay
735 // the MachineModuleInfo address on to DwarfWriter.
736 // AsmPrinter::doInitialization did this analysis.
737 MMI = getAnalysisToUpdate<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000738 DW = getAnalysisToUpdate<DwarfWriter>();
739 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen06545922008-07-09 20:55:35 +0000740 }
741
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000742 // Darwin wants symbols to be quoted if they have complex names.
743 if (Subtarget->isTargetDarwin())
744 Mang->setUseQuotes(true);
745
746 return Result;
747}
748
749
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000750void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000751 const TargetData *TD = TM.getTargetData();
752
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000753 if (!GVar->hasInitializer())
754 return; // External global require no code
755
756 // Check to see if this is a special global used by LLVM, if so, emit it.
757 if (EmitSpecialLLVMGlobal(GVar)) {
758 if (Subtarget->isTargetDarwin() &&
759 TM.getRelocationModel() == Reloc::Static) {
760 if (GVar->getName() == "llvm.global_ctors")
761 O << ".reference .constructors_used\n";
762 else if (GVar->getName() == "llvm.global_dtors")
763 O << ".reference .destructors_used\n";
764 }
765 return;
766 }
767
768 std::string name = Mang->getValueName(GVar);
769 Constant *C = GVar->getInitializer();
770 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000771 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000772 unsigned Align = TD->getPreferredAlignmentLog(GVar);
773
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000774 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000775
776 if (Subtarget->isTargetELF())
777 O << "\t.type\t" << name << ",@object\n";
778
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000779 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000780
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000781 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000782 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000783 if (GVar->hasExternalLinkage()) {
784 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000785 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000786 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000787 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000788 return;
789 }
790 }
791
792 if (!GVar->isThreadLocal() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000793 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000794 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000795
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000796 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000797 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000798 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000799 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000800 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000801 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000802 O << "\t.globl " << name << '\n'
803 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000804 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000805 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 EmitGlobalConstant(C);
809 return;
810 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000811 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000812 if (TAI->getCOMMDirectiveTakesAlignment())
813 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000814 }
815 } else {
816 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000817 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000818 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000819 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000820 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000826 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000827 return;
828 }
829 }
830
831 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000832 case GlobalValue::CommonLinkage:
833 case GlobalValue::LinkOnceLinkage:
834 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000836 O << "\t.globl " << name << '\n'
837 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000840 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000842 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 }
844 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000845 case GlobalValue::DLLExportLinkage:
846 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000847 // FIXME: appending linkage variables should go into a section of
848 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000849 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000851 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000853 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000854 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000856 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 assert(0 && "Unknown linkage type!");
858 }
859
860 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000861 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000862 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000863 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000864 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000865 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866
867 // If the initializer is a extern weak symbol, remember to emit the weak
868 // reference!
869 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
870 if (GV->hasExternalWeakLinkage())
871 ExtWeakSymbols.insert(GV);
872
873 EmitGlobalConstant(C);
874}
875
Evan Cheng76443dc2008-07-08 00:55:58 +0000876/// printGVStub - Print stub for a global value.
877///
878void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000879 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000880 O << ":\n\t.indirect_symbol ";
881 if (Prefix) O << Prefix;
882 O << GV << "\n\t.long\t0\n";
883}
884
Evan Chenga65854f2008-12-05 01:06:39 +0000885/// printHiddenGVStub - Print stub for a hidden global value.
886///
887void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
888 EmitAlignment(2);
889 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
890 if (Prefix) O << Prefix;
891 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
892}
893
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000894
895bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000896 // Print out module-level global variables here.
897 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000898 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000899 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000900
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000901 if (I->hasDLLExportLinkage())
902 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
903 }
904
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000905 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000906 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000907 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000908
909 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
910 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000911 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000912 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000913
914 if (!DLLExportedFns.empty()) {
915 SwitchToDataSection(".section .drectve");
916 }
917
918 for (StringSet<>::iterator i = DLLExportedFns.begin(),
919 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000920 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000921 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000922
923 if (Subtarget->isTargetDarwin()) {
924 SwitchToDataSection("");
925
926 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000927 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +0000928 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000929 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
930 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000931 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000932 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000933 O << ":\n"
934 "\t.indirect_symbol " << p << "\n"
935 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000936 }
937
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000938 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000939
Evan Cheng76443dc2008-07-08 00:55:58 +0000940 // Print global value stubs.
941 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000942 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
943 // Add the (possibly multiple) personalities to the set of global values.
944 // Only referenced functions get into the Personalities list.
945 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000946 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000947 E = Personalities.end(); I != E; ++I) {
948 if (!*I)
949 continue;
950 if (!InStubSection) {
951 SwitchToDataSection(
952 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
953 InStubSection = true;
954 }
955 printGVStub((*I)->getNameStart(), "_");
956 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000957 }
958
959 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000960 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000961 SwitchToDataSection(
962 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
963 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000964 i != e; ++i)
965 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000966
Evan Chenga65854f2008-12-05 01:06:39 +0000967 if (!HiddenGVStubs.empty()) {
968 SwitchToSection(TAI->getDataSection());
969 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
970 i != e; ++i)
971 printHiddenGVStub(i->getKeyData());
972 }
973
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000974 // Emit final debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000975 DwarfWriter *DW = getAnalysisToUpdate<DwarfWriter>();
976 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000977
978 // Funny Darwin hack: This flag tells the linker that no global symbols
979 // contain code that falls through to other global symbols (e.g. the obvious
980 // implementation of multiple entry points). If this doesn't occur, the
981 // linker can safely perform dead code stripping. Since LLVM never
982 // generates code that does this, it is always safe to set.
983 O << "\t.subsections_via_symbols\n";
984 } else if (Subtarget->isTargetCygMing()) {
985 // Emit type information for external functions
986 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
987 i != e; ++i) {
988 O << "\t.def\t " << i->getKeyData()
989 << ";\t.scl\t" << COFF::C_EXT
990 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
991 << ";\t.endef\n";
992 }
993
994 // Emit final debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000995 DwarfWriter *DW = getAnalysisToUpdate<DwarfWriter>();
996 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000997 } else if (Subtarget->isTargetELF()) {
998 // Emit final debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000999 DwarfWriter *DW = getAnalysisToUpdate<DwarfWriter>();
1000 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001001 }
1002
1003 return AsmPrinter::doFinalization(M);
1004}
1005
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006// Include the auto-generated portion of the assembly writer.
1007#include "X86GenAsmWriter.inc"