blob: 5378967b658dd38f090637aed7355cf25285c2a4 [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.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000162 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 break;
164 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000166 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000167 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 break;
169 case Function::LinkOnceLinkage:
170 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000171 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000173 O << "\t.globl\t" << CurrentFnName << '\n';
174 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << "\n"
177 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000179 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 }
181 break;
182 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000183
184 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
186 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000187 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 else if (Subtarget->isTargetCygMing()) {
189 O << "\t.def\t " << CurrentFnName
190 << ";\t.scl\t" <<
191 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
192 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
193 << ";\t.endef\n";
194 }
195
196 O << CurrentFnName << ":\n";
197 // Add some workaround for linkonce linkage on Cygwin\MinGW
198 if (Subtarget->isTargetCygMing() &&
199 (F->getLinkage() == Function::LinkOnceLinkage ||
200 F->getLinkage() == Function::WeakLinkage))
201 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000202}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000204/// runOnMachineFunction - This uses the printMachineInstruction()
205/// method to print assembly for each instruction.
206///
207bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
208 const Function *F = MF.getFunction();
209 unsigned CC = F->getCallingConv();
210
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000211 SetupMachineFunction(MF);
212 O << "\n\n";
213
214 // Populate function information map. Actually, We don't want to populate
215 // non-stdcall or non-fastcall functions' information right now.
216 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
217 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
218
219 // Print out constants referenced by the function
220 EmitConstantPool(MF.getConstantPool());
221
222 if (F->hasDLLExportLinkage())
223 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
224
225 // Print the 'header' of function
226 emitFunctionHeader(MF);
227
228 // Emit pre-function debug and/or EH information.
229 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000230 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000233 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
235 I != E; ++I) {
236 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000237 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000238 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 O << '\n';
240 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000241 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
242 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000244 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000245 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 printMachineInstruction(II);
247 }
248 }
249
Dale Johannesenf35771f2008-04-08 00:37:56 +0000250 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
251 // If the function is empty, then we need to emit *something*. Otherwise,
252 // the function's label might be associated with something that it wasn't
253 // meant to be associated with. We emit a noop in this situation.
254 // We are assuming inline asms are code.
255 O << "\tnop\n";
256 }
257
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000259 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000261 // Emit post-function debug information.
262 if (TAI->doesSupportDebugInformation())
Devang Patelaa1e8432009-01-08 23:40:34 +0000263 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264
265 // Print out jump tables referenced by the function.
266 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000267
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000268 O.flush();
269
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 // We didn't modify anything.
271 return false;
272}
273
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000274static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
276}
277
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000278static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
279 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
280 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
281}
282
283static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
285}
286
287void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
288 const char *Modifier, bool NotRIPRel) {
289 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 switch (MO.getType()) {
291 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000292 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 "Virtual registers should not make it this far!");
294 O << '%';
295 unsigned Reg = MO.getReg();
296 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000297 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
299 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
300 Reg = getX86SubSuperRegister(Reg, VT);
301 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000302 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 return;
304 }
305
306 case MachineOperand::MO_Immediate:
307 if (!Modifier ||
308 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
309 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000310 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 return;
312 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000313 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 return;
315 case MachineOperand::MO_JumpTableIndex: {
316 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
317 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000318 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000319 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
321 if (TM.getRelocationModel() == Reloc::PIC_) {
322 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000323 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
324 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 else if (Subtarget->isPICStyleGOT())
326 O << "@GOTOFF";
327 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000328
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
330 O << "(%rip)";
331 return;
332 }
333 case MachineOperand::MO_ConstantPoolIndex: {
334 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
335 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000336 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000337 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 if (TM.getRelocationModel() == Reloc::PIC_) {
340 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000341 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
342 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 else if (Subtarget->isPICStyleGOT())
344 O << "@GOTOFF";
345 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000346
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000347 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348
349 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
350 O << "(%rip)";
351 return;
352 }
353 case MachineOperand::MO_GlobalAddress: {
354 bool isCallOp = Modifier && !strcmp(Modifier, "call");
355 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
356 bool needCloseParen = false;
357
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000358 const GlobalValue *GV = MO.getGlobal();
359 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
360 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000361 // If GV is an alias then use the aliasee for determining
362 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000363 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000364 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000365 }
366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 bool isThreadLocal = GVar && GVar->isThreadLocal();
368
369 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000370 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 if (!isMemOp && !isCallOp)
373 O << '$';
374 else if (Name[0] == '$') {
375 // The name begins with a dollar-sign. In order to avoid having it look
376 // like an integer immediate to the assembler, enclose it in parens.
377 O << '(';
378 needCloseParen = true;
379 }
380
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000381 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 // Link-once, declaration, or Weakly-linked global variables need
383 // non-lazily-resolved stubs
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000384 if (GV->isDeclaration() || GV->mayBeOverridden()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 // Dynamically-resolved functions need a stub for the function.
386 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000387 // Function stubs are no longer needed for Mac OS X 10.5 and up.
388 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
389 O << Name;
390 } else {
391 FnStubs.insert(Name);
392 printSuffixedName(Name, "$stub");
393 }
Evan Chenga65854f2008-12-05 01:06:39 +0000394 } else if (GV->hasHiddenVisibility()) {
395 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
396 // Definition is not definitely in the current translation unit.
397 O << Name;
398 else {
399 HiddenGVStubs.insert(Name);
400 printSuffixedName(Name, "$non_lazy_ptr");
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
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000436 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437
438 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000439 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 O << "@TLSGD"; // general dynamic TLS model
441 else
442 if (GV->isDeclaration())
443 O << "@INDNTPOFF"; // initial exec TLS model
444 else
445 O << "@NTPOFF"; // local exec TLS model
446 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000447 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
449 O << "@GOT";
450 else
451 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000452 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
453 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000454 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 O << "@GOTPCREL";
456
457 if (needCloseParen) {
458 needCloseParen = false;
459 O << ')';
460 }
461
462 // Use rip when possible to reduce code size, except when
463 // index or base register are also part of the address. e.g.
464 // foo(%rip)(%rcx,%rax,4) is not legal
465 O << "(%rip)";
466 }
467 }
468
469 if (needCloseParen)
470 O << ')';
471
472 return;
473 }
474 case MachineOperand::MO_ExternalSymbol: {
475 bool isCallOp = Modifier && !strcmp(Modifier, "call");
476 bool needCloseParen = false;
477 std::string Name(TAI->getGlobalPrefix());
478 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000479 // Print function stub suffix unless it's Mac OS X 10.5 and up.
480 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
481 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000483 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 return;
485 }
486 if (!isCallOp)
487 O << '$';
488 else if (Name[0] == '$') {
489 // The name begins with a dollar-sign. In order to avoid having it look
490 // like an integer immediate to the assembler, enclose it in parens.
491 O << '(';
492 needCloseParen = true;
493 }
494
495 O << Name;
496
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000497 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 std::string GOTName(TAI->getGlobalPrefix());
499 GOTName+="_GLOBAL_OFFSET_TABLE_";
500 if (Name == GOTName)
501 // HACK! Emit extra offset to PC during printing GOT offset to
502 // compensate for the size of popl instruction. The resulting code
503 // should look like:
504 // call .piclabel
505 // piclabel:
506 // popl %some_register
507 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
508 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000509 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510
511 if (isCallOp)
512 O << "@PLT";
513 }
514
515 if (needCloseParen)
516 O << ')';
517
518 if (!isCallOp && Subtarget->isPICStyleRIPRel())
519 O << "(%rip)";
520
521 return;
522 }
523 default:
524 O << "<unknown operand type>"; return;
525 }
526}
527
528void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000529 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 assert(value <= 7 && "Invalid ssecc argument!");
531 switch (value) {
532 case 0: O << "eq"; break;
533 case 1: O << "lt"; break;
534 case 2: O << "le"; break;
535 case 3: O << "unord"; break;
536 case 4: O << "neq"; break;
537 case 5: O << "nlt"; break;
538 case 6: O << "nle"; break;
539 case 7: O << "ord"; break;
540 }
541}
542
543void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
544 const char *Modifier){
545 assert(isMem(MI, Op) && "Invalid memory reference!");
546 MachineOperand BaseReg = MI->getOperand(Op);
547 MachineOperand IndexReg = MI->getOperand(Op+2);
548 const MachineOperand &DispSpec = MI->getOperand(Op+3);
549
550 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000551 if (DispSpec.isGlobal() ||
552 DispSpec.isCPI() ||
553 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 printOperand(MI, Op+3, "mem", NotRIPRel);
555 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000556 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
558 O << DispVal;
559 }
560
561 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000562 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000564
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 // There are cases where we can end up with ESP/RSP in the indexreg slot.
566 // If this happens, swap the base/index register to support assemblers that
567 // don't work when the index is *SP.
568 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
569 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
570 std::swap(BaseReg, IndexReg);
571 std::swap(BaseRegOperand, IndexRegOperand);
572 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000573
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000574 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 if (BaseReg.getReg())
576 printOperand(MI, Op+BaseRegOperand, Modifier);
577
578 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000579 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 printOperand(MI, Op+IndexRegOperand, Modifier);
581 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000582 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000584 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 }
586}
587
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000588void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000589 const MachineBasicBlock *MBB) const {
590 if (!TAI->getSetDirective())
591 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000592
593 // We don't need .set machinery if we have GOT-style relocations
594 if (Subtarget->isPICStyleGOT())
595 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000596
Evan Cheng6fb06762007-11-09 01:32:10 +0000597 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
598 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000599 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000600 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000601 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000602 << '_' << uid << '\n';
603 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000604 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000605}
606
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000608 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000609 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610}
611
612
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000613void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
614 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000615 unsigned uid) const
616{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000617 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
618 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
619
620 O << JTEntryDirective << ' ';
621
622 if (TM.getRelocationModel() == Reloc::PIC_) {
623 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
624 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
625 << '_' << uid << "_set_" << MBB->getNumber();
626 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000627 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000628 O << "@GOTOFF";
629 } else
630 assert(0 && "Don't know how to print MBB label for this PIC mode");
631 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000632 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000633}
634
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000635bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 unsigned Reg = MO.getReg();
638 switch (Mode) {
639 default: return true; // Unknown mode.
640 case 'b': // Print QImode register
641 Reg = getX86SubSuperRegister(Reg, MVT::i8);
642 break;
643 case 'h': // Print QImode high register
644 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
645 break;
646 case 'w': // Print HImode register
647 Reg = getX86SubSuperRegister(Reg, MVT::i16);
648 break;
649 case 'k': // Print SImode register
650 Reg = getX86SubSuperRegister(Reg, MVT::i32);
651 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000652 case 'q': // Print DImode register
653 Reg = getX86SubSuperRegister(Reg, MVT::i64);
654 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 }
656
Evan Cheng00d04a72008-07-07 22:21:06 +0000657 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 return false;
659}
660
661/// PrintAsmOperand - Print out an operand for an inline asm expression.
662///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000663bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000664 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 const char *ExtraCode) {
666 // Does this asm operand have a single letter operand modifier?
667 if (ExtraCode && ExtraCode[0]) {
668 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000669
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 switch (ExtraCode[0]) {
671 default: return true; // Unknown modifier.
672 case 'c': // Don't print "$" before a global var name or constant.
673 printOperand(MI, OpNo, "mem");
674 return false;
675 case 'b': // Print QImode register
676 case 'h': // Print QImode high register
677 case 'w': // Print HImode register
678 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000679 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000680 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
682 printOperand(MI, OpNo);
683 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000684
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 case 'P': // Don't print @PLT, but do print as memory.
686 printOperand(MI, OpNo, "mem");
687 return false;
688 }
689 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000690
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 printOperand(MI, OpNo);
692 return false;
693}
694
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000695bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000697 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000699 if (ExtraCode && ExtraCode[0]) {
700 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000701
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000702 switch (ExtraCode[0]) {
703 default: return true; // Unknown modifier.
704 case 'b': // Print QImode register
705 case 'h': // Print QImode high register
706 case 'w': // Print HImode register
707 case 'k': // Print SImode register
708 case 'q': // Print SImode register
709 // These only apply to registers, ignore on mem.
710 break;
711 }
712 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 printMemReference(MI, OpNo);
714 return false;
715}
716
717/// printMachineInstruction -- Print out a single X86 LLVM instruction
718/// MI in AT&T syntax to the current output stream.
719///
720void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
721 ++EmittedInsts;
722
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 // Call the autogenerated instruction printer routines.
724 printInstruction(MI);
725}
726
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000727/// doInitialization
728bool X86ATTAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000729
Evan Cheng5cda7762008-07-09 06:36:53 +0000730 bool Result = AsmPrinter::doInitialization(M);
731
Dale Johannesen06545922008-07-09 20:55:35 +0000732 if (TAI->doesSupportDebugInformation()) {
733 // Let PassManager know we need debug information and relay
734 // the MachineModuleInfo address on to DwarfWriter.
735 // AsmPrinter::doInitialization did this analysis.
736 MMI = getAnalysisToUpdate<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000737 DW = getAnalysisToUpdate<DwarfWriter>();
738 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen06545922008-07-09 20:55:35 +0000739 }
740
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000741 // Darwin wants symbols to be quoted if they have complex names.
742 if (Subtarget->isTargetDarwin())
743 Mang->setUseQuotes(true);
744
745 return Result;
746}
747
748
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000749void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000750 const TargetData *TD = TM.getTargetData();
751
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000752 if (!GVar->hasInitializer())
753 return; // External global require no code
754
755 // Check to see if this is a special global used by LLVM, if so, emit it.
756 if (EmitSpecialLLVMGlobal(GVar)) {
757 if (Subtarget->isTargetDarwin() &&
758 TM.getRelocationModel() == Reloc::Static) {
759 if (GVar->getName() == "llvm.global_ctors")
760 O << ".reference .constructors_used\n";
761 else if (GVar->getName() == "llvm.global_dtors")
762 O << ".reference .destructors_used\n";
763 }
764 return;
765 }
766
767 std::string name = Mang->getValueName(GVar);
768 Constant *C = GVar->getInitializer();
769 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000770 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000771 unsigned Align = TD->getPreferredAlignmentLog(GVar);
772
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000773 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000774
775 if (Subtarget->isTargetELF())
776 O << "\t.type\t" << name << ",@object\n";
777
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000778 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000779
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000780 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000781 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000782 if (GVar->hasExternalLinkage()) {
783 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000784 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000785 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000786 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000787 return;
788 }
789 }
790
791 if (!GVar->isThreadLocal() &&
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000792 (GVar->hasInternalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000793 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000794
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 if (TAI->getLCOMMDirective() != NULL) {
796 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000797 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000798 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000799 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000800 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000801 O << "\t.globl " << name << '\n'
802 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000803 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000804 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000806 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000807 EmitGlobalConstant(C);
808 return;
809 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000810 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000811 if (TAI->getCOMMDirectiveTakesAlignment())
812 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 }
814 } else {
815 if (!Subtarget->isTargetCygMing()) {
816 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000817 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000818 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000819 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000821 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000822 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000823 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000824 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000825 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 return;
827 }
828 }
829
830 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000831 case GlobalValue::CommonLinkage:
832 case GlobalValue::LinkOnceLinkage:
833 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << "\t.globl " << name << '\n'
836 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000837 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000839 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000840 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000841 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000842 }
843 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000844 case GlobalValue::DLLExportLinkage:
845 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846 // FIXME: appending linkage variables should go into a section of
847 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000848 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000850 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000851 // FALL THROUGH
Evan Cheng630c5612008-08-08 06:43:59 +0000852 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000853 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000854 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 assert(0 && "Unknown linkage type!");
856 }
857
858 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000859 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000860 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000861 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000862 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000863 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000864
865 // If the initializer is a extern weak symbol, remember to emit the weak
866 // reference!
867 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
868 if (GV->hasExternalWeakLinkage())
869 ExtWeakSymbols.insert(GV);
870
871 EmitGlobalConstant(C);
872}
873
Evan Cheng76443dc2008-07-08 00:55:58 +0000874/// printGVStub - Print stub for a global value.
875///
876void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000877 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000878 O << ":\n\t.indirect_symbol ";
879 if (Prefix) O << Prefix;
880 O << GV << "\n\t.long\t0\n";
881}
882
Evan Chenga65854f2008-12-05 01:06:39 +0000883/// printHiddenGVStub - Print stub for a hidden global value.
884///
885void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
886 EmitAlignment(2);
887 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
888 if (Prefix) O << Prefix;
889 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
890}
891
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892
893bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000894 // Print out module-level global variables here.
895 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000896 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000897 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000898
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000899 if (I->hasDLLExportLinkage())
900 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
901 }
902
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000903 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000904 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000905 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000906
907 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
908 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000909 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000910 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000911
912 if (!DLLExportedFns.empty()) {
913 SwitchToDataSection(".section .drectve");
914 }
915
916 for (StringSet<>::iterator i = DLLExportedFns.begin(),
917 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000918 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000919 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000920
921 if (Subtarget->isTargetDarwin()) {
922 SwitchToDataSection("");
923
924 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000925 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +0000926 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000927 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
Evan Chenga65854f2008-12-05 01:06:39 +0000965 if (!HiddenGVStubs.empty()) {
966 SwitchToSection(TAI->getDataSection());
967 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
968 i != e; ++i)
969 printHiddenGVStub(i->getKeyData());
970 }
971
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000972 // Emit final debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000973 DwarfWriter *DW = getAnalysisToUpdate<DwarfWriter>();
974 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000975
976 // Funny Darwin hack: This flag tells the linker that no global symbols
977 // contain code that falls through to other global symbols (e.g. the obvious
978 // implementation of multiple entry points). If this doesn't occur, the
979 // linker can safely perform dead code stripping. Since LLVM never
980 // generates code that does this, it is always safe to set.
981 O << "\t.subsections_via_symbols\n";
982 } else if (Subtarget->isTargetCygMing()) {
983 // Emit type information for external functions
984 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
985 i != e; ++i) {
986 O << "\t.def\t " << i->getKeyData()
987 << ";\t.scl\t" << COFF::C_EXT
988 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
989 << ";\t.endef\n";
990 }
991
992 // Emit final debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000993 DwarfWriter *DW = getAnalysisToUpdate<DwarfWriter>();
994 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000995 } else if (Subtarget->isTargetELF()) {
996 // Emit final debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000997 DwarfWriter *DW = getAnalysisToUpdate<DwarfWriter>();
998 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000999 }
1000
1001 return AsmPrinter::doFinalization(M);
1002}
1003
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004// Include the auto-generated portion of the assembly writer.
1005#include "X86GenAsmWriter.inc"