blob: 0595b6fbd1390e511db766afbad4a4a6cfe0186c [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"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000029#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000030#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetAsmInfo.h"
34#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Evan Cheng0729ccf2008-01-05 00:41:47 +000039static std::string getPICLabelString(unsigned FnNum,
40 const TargetAsmInfo *TAI,
41 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 std::string label;
43 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000044 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000046 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 else
48 assert(0 && "Don't know how to print PIC label!\n");
49
50 return label;
51}
52
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000053static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
54 const TargetData *TD) {
55 X86MachineFunctionInfo Info;
56 uint64_t Size = 0;
57
58 switch (F->getCallingConv()) {
59 case CallingConv::X86_StdCall:
60 Info.setDecorationStyle(StdCall);
61 break;
62 case CallingConv::X86_FastCall:
63 Info.setDecorationStyle(FastCall);
64 break;
65 default:
66 return Info;
67 }
68
69 unsigned argNum = 1;
70 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
71 AI != AE; ++AI, ++argNum) {
72 const Type* Ty = AI->getType();
73
74 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000075 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000076 Ty = cast<PointerType>(Ty)->getElementType();
77
78 // Size should be aligned to DWORD boundary
Duncan Sandsd68f13b2009-01-12 20:38:59 +000079 Size += ((TD->getTypePaddedSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000080 }
81
82 // We're not supporting tooooo huge arguments :)
83 Info.setBytesToPopOnReturn((unsigned int)Size);
84 return Info;
85}
86
87/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +000088/// Don't print things like \\n or \\0.
Owen Anderson847b99b2008-08-21 00:14:44 +000089static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000090 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
91 Name != E; ++Name)
92 if (isprint(*Name))
93 OS << *Name;
94}
95
96/// decorateName - Query FunctionInfoMap and use this information for various
97/// name decoration.
98void X86ATTAsmPrinter::decorateName(std::string &Name,
99 const GlobalValue *GV) {
100 const Function *F = dyn_cast<Function>(GV);
101 if (!F) return;
102
103 // We don't want to decorate non-stdcall or non-fastcall functions right now
104 unsigned CC = F->getCallingConv();
105 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
106 return;
107
108 // Decorate names only when we're targeting Cygwin/Mingw32 targets
109 if (!Subtarget->isTargetCygMing())
110 return;
111
112 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
113
114 const X86MachineFunctionInfo *Info;
115 if (info_item == FunctionInfoMap.end()) {
116 // Calculate apropriate function info and populate map
117 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
118 Info = &FunctionInfoMap[F];
119 } else {
120 Info = &info_item->second;
121 }
122
123 const FunctionType *FT = F->getFunctionType();
124 switch (Info->getDecorationStyle()) {
125 case None:
126 break;
127 case StdCall:
128 // "Pure" variadic functions do not receive @0 suffix.
129 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
131 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
132 break;
133 case FastCall:
134 // "Pure" variadic functions do not receive @0 suffix.
135 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
137 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
138
139 if (Name[0] == '_') {
140 Name[0] = '@';
141 } else {
142 Name = '@' + Name;
143 }
144 break;
145 default:
146 assert(0 && "Unsupported DecorationStyle");
147 }
148}
149
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000150void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000153 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000155 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000156
Devang Patel93698d92008-10-01 23:18:38 +0000157 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000158 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000159 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 switch (F->getLinkage()) {
161 default: assert(0 && "Unknown linkage type!");
162 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000163 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000164 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 break;
166 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000168 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000169 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
171 case Function::LinkOnceLinkage:
172 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000173 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000175 O << "\t.globl\t" << CurrentFnName << '\n';
176 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000178 O << "\t.globl\t" << CurrentFnName << "\n"
179 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 }
183 break;
184 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000185
186 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187
188 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000189 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 else if (Subtarget->isTargetCygMing()) {
191 O << "\t.def\t " << CurrentFnName
192 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000193 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
195 << ";\t.endef\n";
196 }
197
198 O << CurrentFnName << ":\n";
199 // Add some workaround for linkonce linkage on Cygwin\MinGW
200 if (Subtarget->isTargetCygMing() &&
201 (F->getLinkage() == Function::LinkOnceLinkage ||
202 F->getLinkage() == Function::WeakLinkage))
203 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000204}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000206/// runOnMachineFunction - This uses the printMachineInstruction()
207/// method to print assembly for each instruction.
208///
209bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
210 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000211 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212 unsigned CC = F->getCallingConv();
213
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000214 SetupMachineFunction(MF);
215 O << "\n\n";
216
217 // Populate function information map. Actually, We don't want to populate
218 // non-stdcall or non-fastcall functions' information right now.
219 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
220 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
221
222 // Print out constants referenced by the function
223 EmitConstantPool(MF.getConstantPool());
224
225 if (F->hasDLLExportLinkage())
226 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
227
228 // Print the 'header' of function
229 emitFunctionHeader(MF);
230
231 // Emit pre-function debug and/or EH information.
232 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000233 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000236 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
238 I != E; ++I) {
239 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000240 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000241 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 O << '\n';
243 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000244 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
245 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000247 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000248 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 printMachineInstruction(II);
250 }
251 }
252
Dale Johannesenf35771f2008-04-08 00:37:56 +0000253 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
254 // If the function is empty, then we need to emit *something*. Otherwise,
255 // the function's label might be associated with something that it wasn't
256 // meant to be associated with. We emit a noop in this situation.
257 // We are assuming inline asms are code.
258 O << "\tnop\n";
259 }
260
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000262 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000264 // Emit post-function debug information.
265 if (TAI->doesSupportDebugInformation())
Devang Patelaa1e8432009-01-08 23:40:34 +0000266 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267
268 // Print out jump tables referenced by the function.
269 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000270
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000271 O.flush();
272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 // We didn't modify anything.
274 return false;
275}
276
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000277static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
279}
280
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000281static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
282 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
283 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
284}
285
286static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
288}
289
290void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
291 const char *Modifier, bool NotRIPRel) {
292 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 switch (MO.getType()) {
294 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000295 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 "Virtual registers should not make it this far!");
297 O << '%';
298 unsigned Reg = MO.getReg();
299 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000300 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
302 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
303 Reg = getX86SubSuperRegister(Reg, VT);
304 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000305 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 return;
307 }
308
309 case MachineOperand::MO_Immediate:
Evan Cheng75952172009-03-04 06:48:53 +0000310 if (Modifier && !strcmp(Modifier, "call"))
311 O << '*';
312 else if (!Modifier ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
314 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000315 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 return;
317 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000318 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 return;
320 case MachineOperand::MO_JumpTableIndex: {
321 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
322 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000323 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000324 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 if (TM.getRelocationModel() == Reloc::PIC_) {
327 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000328 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
329 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 else if (Subtarget->isPICStyleGOT())
331 O << "@GOTOFF";
332 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000333
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
335 O << "(%rip)";
336 return;
337 }
338 case MachineOperand::MO_ConstantPoolIndex: {
339 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
340 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000341 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000342 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 if (TM.getRelocationModel() == Reloc::PIC_) {
345 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000346 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
347 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 else if (Subtarget->isPICStyleGOT())
349 O << "@GOTOFF";
350 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000351
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000352 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353
354 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
355 O << "(%rip)";
356 return;
357 }
358 case MachineOperand::MO_GlobalAddress: {
359 bool isCallOp = Modifier && !strcmp(Modifier, "call");
360 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
361 bool needCloseParen = false;
362
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000363 const GlobalValue *GV = MO.getGlobal();
364 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
365 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000366 // If GV is an alias then use the aliasee for determining
367 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000368 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000369 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000370 }
371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 bool isThreadLocal = GVar && GVar->isThreadLocal();
373
374 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000375 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 if (!isMemOp && !isCallOp)
378 O << '$';
379 else if (Name[0] == '$') {
380 // The name begins with a dollar-sign. In order to avoid having it look
381 // like an integer immediate to the assembler, enclose it in parens.
382 O << '(';
383 needCloseParen = true;
384 }
385
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000386 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 // Link-once, declaration, or Weakly-linked global variables need
388 // non-lazily-resolved stubs
Duncan Sandseb3f45f2008-09-29 11:25:42 +0000389 if (GV->isDeclaration() || GV->mayBeOverridden()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 // Dynamically-resolved functions need a stub for the function.
391 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000392 // Function stubs are no longer needed for Mac OS X 10.5 and up.
393 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
394 O << Name;
395 } else {
396 FnStubs.insert(Name);
397 printSuffixedName(Name, "$stub");
398 }
Evan Chenga65854f2008-12-05 01:06:39 +0000399 } else if (GV->hasHiddenVisibility()) {
400 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
401 // Definition is not definitely in the current translation unit.
402 O << Name;
403 else {
404 HiddenGVStubs.insert(Name);
405 printSuffixedName(Name, "$non_lazy_ptr");
406 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 } else {
408 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000409 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 }
411 } else {
412 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000413 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 O << Name;
415 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000416
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000418 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 } else {
420 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000421 O << "__imp_";
422 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 O << Name;
424
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000425 if (isCallOp) {
426 if (shouldPrintPLT(TM, Subtarget)) {
427 // Assemble call via PLT for externally visible symbols
428 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000429 !GV->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 O << "@PLT";
431 }
432 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
433 // Save function name for later type emission
434 FnStubs.insert(Name);
435 }
436 }
437
438 if (GV->hasExternalWeakLinkage())
439 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000440
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000441 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
443 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000444 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
445 switch (model) {
446 case TLSModel::GeneralDynamic:
447 O << "@TLSGD";
448 break;
449 case TLSModel::LocalDynamic:
450 // O << "@TLSLD"; // local dynamic not implemented
451 O << "@TLSGD";
452 break;
453 case TLSModel::InitialExec:
454 if (Subtarget->is64Bit())
455 O << "@TLSGD"; // 64 bit intial exec not implemented
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 else
Rafael Espindola7b620af2009-02-27 13:37:18 +0000457 O << "@INDNTPOFF";
458 break;
459 case TLSModel::LocalExec:
460 if (Subtarget->is64Bit())
461 O << "@TLSGD"; // 64 bit local exec not implemented
462 else
463 O << "@NTPOFF";
464 break;
465 default:
466 assert (0 && "Unknown TLS model");
467 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000469 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
471 O << "@GOT";
472 else
473 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000474 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
475 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000476 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 O << "@GOTPCREL";
478
479 if (needCloseParen) {
480 needCloseParen = false;
481 O << ')';
482 }
483
484 // Use rip when possible to reduce code size, except when
485 // index or base register are also part of the address. e.g.
486 // foo(%rip)(%rcx,%rax,4) is not legal
487 O << "(%rip)";
488 }
489 }
490
491 if (needCloseParen)
492 O << ')';
493
494 return;
495 }
496 case MachineOperand::MO_ExternalSymbol: {
497 bool isCallOp = Modifier && !strcmp(Modifier, "call");
498 bool needCloseParen = false;
499 std::string Name(TAI->getGlobalPrefix());
500 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000501 // Print function stub suffix unless it's Mac OS X 10.5 and up.
502 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
503 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000505 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 return;
507 }
508 if (!isCallOp)
509 O << '$';
510 else if (Name[0] == '$') {
511 // The name begins with a dollar-sign. In order to avoid having it look
512 // like an integer immediate to the assembler, enclose it in parens.
513 O << '(';
514 needCloseParen = true;
515 }
516
517 O << Name;
518
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000519 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 std::string GOTName(TAI->getGlobalPrefix());
521 GOTName+="_GLOBAL_OFFSET_TABLE_";
522 if (Name == GOTName)
523 // HACK! Emit extra offset to PC during printing GOT offset to
524 // compensate for the size of popl instruction. The resulting code
525 // should look like:
526 // call .piclabel
527 // piclabel:
528 // popl %some_register
529 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
530 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000531 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532
533 if (isCallOp)
534 O << "@PLT";
535 }
536
537 if (needCloseParen)
538 O << ')';
539
540 if (!isCallOp && Subtarget->isPICStyleRIPRel())
541 O << "(%rip)";
542
543 return;
544 }
545 default:
546 O << "<unknown operand type>"; return;
547 }
548}
549
550void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000551 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 assert(value <= 7 && "Invalid ssecc argument!");
553 switch (value) {
554 case 0: O << "eq"; break;
555 case 1: O << "lt"; break;
556 case 2: O << "le"; break;
557 case 3: O << "unord"; break;
558 case 4: O << "neq"; break;
559 case 5: O << "nlt"; break;
560 case 6: O << "nle"; break;
561 case 7: O << "ord"; break;
562 }
563}
564
565void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
566 const char *Modifier){
567 assert(isMem(MI, Op) && "Invalid memory reference!");
568 MachineOperand BaseReg = MI->getOperand(Op);
569 MachineOperand IndexReg = MI->getOperand(Op+2);
570 const MachineOperand &DispSpec = MI->getOperand(Op+3);
571
572 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000573 if (DispSpec.isGlobal() ||
574 DispSpec.isCPI() ||
575 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 printOperand(MI, Op+3, "mem", NotRIPRel);
577 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000578 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
580 O << DispVal;
581 }
582
583 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000584 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000586
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 // There are cases where we can end up with ESP/RSP in the indexreg slot.
588 // If this happens, swap the base/index register to support assemblers that
589 // don't work when the index is *SP.
590 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
591 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
592 std::swap(BaseReg, IndexReg);
593 std::swap(BaseRegOperand, IndexRegOperand);
594 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000595
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000596 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 if (BaseReg.getReg())
598 printOperand(MI, Op+BaseRegOperand, Modifier);
599
600 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000601 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 printOperand(MI, Op+IndexRegOperand, Modifier);
603 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000604 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000606 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 }
608}
609
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000610void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000611 const MachineBasicBlock *MBB) const {
612 if (!TAI->getSetDirective())
613 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000614
615 // We don't need .set machinery if we have GOT-style relocations
616 if (Subtarget->isPICStyleGOT())
617 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000618
Evan Cheng6fb06762007-11-09 01:32:10 +0000619 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
620 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000621 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000622 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000623 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000624 << '_' << uid << '\n';
625 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000626 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000627}
628
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000630 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000631 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632}
633
634
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000635void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
636 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000637 unsigned uid) const
638{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000639 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
640 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
641
642 O << JTEntryDirective << ' ';
643
644 if (TM.getRelocationModel() == Reloc::PIC_) {
645 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
646 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
647 << '_' << uid << "_set_" << MBB->getNumber();
648 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000649 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000650 O << "@GOTOFF";
651 } else
652 assert(0 && "Don't know how to print MBB label for this PIC mode");
653 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000654 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000655}
656
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000657bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 unsigned Reg = MO.getReg();
660 switch (Mode) {
661 default: return true; // Unknown mode.
662 case 'b': // Print QImode register
663 Reg = getX86SubSuperRegister(Reg, MVT::i8);
664 break;
665 case 'h': // Print QImode high register
666 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
667 break;
668 case 'w': // Print HImode register
669 Reg = getX86SubSuperRegister(Reg, MVT::i16);
670 break;
671 case 'k': // Print SImode register
672 Reg = getX86SubSuperRegister(Reg, MVT::i32);
673 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000674 case 'q': // Print DImode register
675 Reg = getX86SubSuperRegister(Reg, MVT::i64);
676 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 }
678
Evan Cheng00d04a72008-07-07 22:21:06 +0000679 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 return false;
681}
682
683/// PrintAsmOperand - Print out an operand for an inline asm expression.
684///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000685bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000686 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 const char *ExtraCode) {
688 // Does this asm operand have a single letter operand modifier?
689 if (ExtraCode && ExtraCode[0]) {
690 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 switch (ExtraCode[0]) {
693 default: return true; // Unknown modifier.
694 case 'c': // Don't print "$" before a global var name or constant.
695 printOperand(MI, OpNo, "mem");
696 return false;
697 case 'b': // Print QImode register
698 case 'h': // Print QImode high register
699 case 'w': // Print HImode register
700 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000701 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000702 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
704 printOperand(MI, OpNo);
705 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000706
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 case 'P': // Don't print @PLT, but do print as memory.
708 printOperand(MI, OpNo, "mem");
709 return false;
710 }
711 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000712
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 printOperand(MI, OpNo);
714 return false;
715}
716
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000717bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000719 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000721 if (ExtraCode && ExtraCode[0]) {
722 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000723
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000724 switch (ExtraCode[0]) {
725 default: return true; // Unknown modifier.
726 case 'b': // Print QImode register
727 case 'h': // Print QImode high register
728 case 'w': // Print HImode register
729 case 'k': // Print SImode register
730 case 'q': // Print SImode register
731 // These only apply to registers, ignore on mem.
732 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000733 case 'P': // Don't print @PLT, but do print as memory.
734 printOperand(MI, OpNo, "mem");
735 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000736 }
737 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 printMemReference(MI, OpNo);
739 return false;
740}
741
Bill Wendlingb3b11262009-02-06 21:45:08 +0000742/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
743/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744///
745void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
746 ++EmittedInsts;
747
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 // Call the autogenerated instruction printer routines.
749 printInstruction(MI);
750}
751
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000752/// doInitialization
753bool X86ATTAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000754
Evan Cheng5cda7762008-07-09 06:36:53 +0000755 bool Result = AsmPrinter::doInitialization(M);
756
Dale Johannesen06545922008-07-09 20:55:35 +0000757 if (TAI->doesSupportDebugInformation()) {
758 // Let PassManager know we need debug information and relay
759 // the MachineModuleInfo address on to DwarfWriter.
760 // AsmPrinter::doInitialization did this analysis.
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000761 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
762 DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000763 DW->BeginModule(&M, MMI, O, this, TAI);
Dale Johannesen06545922008-07-09 20:55:35 +0000764 }
765
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000766 // Darwin wants symbols to be quoted if they have complex names.
767 if (Subtarget->isTargetDarwin())
768 Mang->setUseQuotes(true);
769
770 return Result;
771}
772
773
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000774void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000775 const TargetData *TD = TM.getTargetData();
776
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000777 if (!GVar->hasInitializer())
778 return; // External global require no code
779
780 // Check to see if this is a special global used by LLVM, if so, emit it.
781 if (EmitSpecialLLVMGlobal(GVar)) {
782 if (Subtarget->isTargetDarwin() &&
783 TM.getRelocationModel() == Reloc::Static) {
784 if (GVar->getName() == "llvm.global_ctors")
785 O << ".reference .constructors_used\n";
786 else if (GVar->getName() == "llvm.global_dtors")
787 O << ".reference .destructors_used\n";
788 }
789 return;
790 }
791
792 std::string name = Mang->getValueName(GVar);
793 Constant *C = GVar->getInitializer();
794 const Type *Type = C->getType();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000795 unsigned Size = TD->getTypePaddedSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000796 unsigned Align = TD->getPreferredAlignmentLog(GVar);
797
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000798 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000799
800 if (Subtarget->isTargetELF())
801 O << "\t.type\t" << name << ",@object\n";
802
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000803 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000804
Evan Chengcf84b142009-02-18 02:19:52 +0000805 if (C->isNullValue() && !GVar->hasSection() &&
806 !(Subtarget->isTargetDarwin() &&
807 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000808 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000809 if (GVar->hasExternalLinkage()) {
810 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000811 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000812 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000813 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000814 return;
815 }
816 }
817
818 if (!GVar->isThreadLocal() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000819 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000821
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000822 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000823 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000826 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000827 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000828 O << "\t.globl " << name << '\n'
829 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000831 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 EmitGlobalConstant(C);
835 return;
836 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000837 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000838 if (TAI->getCOMMDirectiveTakesAlignment())
839 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000840 }
841 } else {
842 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000843 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000844 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000846 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000847 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000848 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000850 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000851 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000852 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000853 return;
854 }
855 }
856
857 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000858 case GlobalValue::CommonLinkage:
859 case GlobalValue::LinkOnceLinkage:
860 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 O << "\t.globl " << name << '\n'
863 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000864 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000866 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000868 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 }
870 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000871 case GlobalValue::DLLExportLinkage:
872 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000873 // FIXME: appending linkage variables should go into a section of
874 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000875 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000876 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000877 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000878 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000879 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000880 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000881 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000882 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883 assert(0 && "Unknown linkage type!");
884 }
885
886 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000887 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000888 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000889 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000890 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000891 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000893 EmitGlobalConstant(C);
894}
895
Evan Cheng76443dc2008-07-08 00:55:58 +0000896/// printGVStub - Print stub for a global value.
897///
898void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000899 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000900 O << ":\n\t.indirect_symbol ";
901 if (Prefix) O << Prefix;
902 O << GV << "\n\t.long\t0\n";
903}
904
Evan Chenga65854f2008-12-05 01:06:39 +0000905/// printHiddenGVStub - Print stub for a hidden global value.
906///
907void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
908 EmitAlignment(2);
909 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
910 if (Prefix) O << Prefix;
911 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
912}
913
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000914
915bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000916 // Print out module-level global variables here.
917 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000918 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000919 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000920
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000921 if (I->hasDLLExportLinkage())
922 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000923
924 // If the global is a extern weak symbol, remember to emit the weak
925 // reference!
926 // FIXME: This is rather hacky, since we'll emit references to ALL weak stuff,
927 // not used. But currently it's the only way to deal with extern weak
928 // initializers hidden deep inside constant expressions.
929 if (I->hasExternalWeakLinkage())
930 ExtWeakSymbols.insert(I);
931 }
932
933 for (Module::const_iterator I = M.begin(), E = M.end();
934 I != E; ++I) {
935 // If the global is a extern weak symbol, remember to emit the weak
936 // reference!
937 // FIXME: This is rather hacky, since we'll emit references to ALL weak stuff,
938 // not used. But currently it's the only way to deal with extern weak
939 // initializers hidden deep inside constant expressions.
940 if (I->hasExternalWeakLinkage())
941 ExtWeakSymbols.insert(I);
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000942 }
943
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000944 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000945 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000946 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000947
948 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
949 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000950 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000951 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000952
953 if (!DLLExportedFns.empty()) {
954 SwitchToDataSection(".section .drectve");
955 }
956
957 for (StringSet<>::iterator i = DLLExportedFns.begin(),
958 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000959 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000960 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000961
962 if (Subtarget->isTargetDarwin()) {
963 SwitchToDataSection("");
964
965 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000966 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +0000967 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000968 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
969 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000970 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000971 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000972 O << ":\n"
973 "\t.indirect_symbol " << p << "\n"
974 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000975 }
976
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000977 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000978
Evan Cheng76443dc2008-07-08 00:55:58 +0000979 // Print global value stubs.
980 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000981 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
982 // Add the (possibly multiple) personalities to the set of global values.
983 // Only referenced functions get into the Personalities list.
984 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000985 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000986 E = Personalities.end(); I != E; ++I) {
987 if (!*I)
988 continue;
989 if (!InStubSection) {
990 SwitchToDataSection(
991 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
992 InStubSection = true;
993 }
994 printGVStub((*I)->getNameStart(), "_");
995 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000996 }
997
998 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000999 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001000 SwitchToDataSection(
1001 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1002 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +00001003 i != e; ++i)
1004 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001005
Evan Chenga65854f2008-12-05 01:06:39 +00001006 if (!HiddenGVStubs.empty()) {
1007 SwitchToSection(TAI->getDataSection());
1008 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1009 i != e; ++i)
1010 printHiddenGVStub(i->getKeyData());
1011 }
1012
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001013 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001014 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001015 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001016
1017 // Funny Darwin hack: This flag tells the linker that no global symbols
1018 // contain code that falls through to other global symbols (e.g. the obvious
1019 // implementation of multiple entry points). If this doesn't occur, the
1020 // linker can safely perform dead code stripping. Since LLVM never
1021 // generates code that does this, it is always safe to set.
1022 O << "\t.subsections_via_symbols\n";
1023 } else if (Subtarget->isTargetCygMing()) {
1024 // Emit type information for external functions
1025 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1026 i != e; ++i) {
1027 O << "\t.def\t " << i->getKeyData()
1028 << ";\t.scl\t" << COFF::C_EXT
1029 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1030 << ";\t.endef\n";
1031 }
1032
1033 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001034 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001035 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001036 } else if (Subtarget->isTargetELF()) {
1037 // Emit final debug information.
Duncan Sands4e0d6a72009-01-28 13:14:17 +00001038 DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>();
Devang Patelaa1e8432009-01-08 23:40:34 +00001039 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001040 }
1041
1042 return AsmPrinter::doFinalization(M);
1043}
1044
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045// Include the auto-generated portion of the assembly writer.
1046#include "X86GenAsmWriter.inc"