blob: 8c688f354fae50989d798147c85d9e22d8e03a7e [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"
18#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"
31#include "llvm/Target/TargetAsmInfo.h"
32#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
Evan Cheng0729ccf2008-01-05 00:41:47 +000037static std::string getPICLabelString(unsigned FnNum,
38 const TargetAsmInfo *TAI,
39 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 std::string label;
41 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000042 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000044 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 else
46 assert(0 && "Don't know how to print PIC label!\n");
47
48 return label;
49}
50
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000051static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
52 const TargetData *TD) {
53 X86MachineFunctionInfo Info;
54 uint64_t Size = 0;
55
56 switch (F->getCallingConv()) {
57 case CallingConv::X86_StdCall:
58 Info.setDecorationStyle(StdCall);
59 break;
60 case CallingConv::X86_FastCall:
61 Info.setDecorationStyle(FastCall);
62 break;
63 default:
64 return Info;
65 }
66
67 unsigned argNum = 1;
68 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
69 AI != AE; ++AI, ++argNum) {
70 const Type* Ty = AI->getType();
71
72 // 'Dereference' type in case of byval parameter attribute
73 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
74 Ty = cast<PointerType>(Ty)->getElementType();
75
76 // Size should be aligned to DWORD boundary
77 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
78 }
79
80 // We're not supporting tooooo huge arguments :)
81 Info.setBytesToPopOnReturn((unsigned int)Size);
82 return Info;
83}
84
85/// PrintUnmangledNameSafely - Print out the printable characters in the name.
86/// Don't print things like \n or \0.
87static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
88 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
89 Name != E; ++Name)
90 if (isprint(*Name))
91 OS << *Name;
92}
93
94/// decorateName - Query FunctionInfoMap and use this information for various
95/// name decoration.
96void X86ATTAsmPrinter::decorateName(std::string &Name,
97 const GlobalValue *GV) {
98 const Function *F = dyn_cast<Function>(GV);
99 if (!F) return;
100
101 // We don't want to decorate non-stdcall or non-fastcall functions right now
102 unsigned CC = F->getCallingConv();
103 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
104 return;
105
106 // Decorate names only when we're targeting Cygwin/Mingw32 targets
107 if (!Subtarget->isTargetCygMing())
108 return;
109
110 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
111
112 const X86MachineFunctionInfo *Info;
113 if (info_item == FunctionInfoMap.end()) {
114 // Calculate apropriate function info and populate map
115 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
116 Info = &FunctionInfoMap[F];
117 } else {
118 Info = &info_item->second;
119 }
120
121 const FunctionType *FT = F->getFunctionType();
122 switch (Info->getDecorationStyle()) {
123 case None:
124 break;
125 case StdCall:
126 // "Pure" variadic functions do not receive @0 suffix.
127 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
128 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
129 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
130 break;
131 case FastCall:
132 // "Pure" variadic functions do not receive @0 suffix.
133 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
134 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
135 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
136
137 if (Name[0] == '_') {
138 Name[0] = '@';
139 } else {
140 Name = '@' + Name;
141 }
142 break;
143 default:
144 assert(0 && "Unsupported DecorationStyle");
145 }
146}
147
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000148// Substitute old hook with new one temporary
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000150 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151}
152
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000153void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 const Function *F = MF.getFunction();
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000155 std::string SectionName = TAI->SectionForGlobal(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000157 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000159 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000160
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000161 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 switch (F->getLinkage()) {
163 default: assert(0 && "Unknown linkage type!");
164 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000165 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 break;
167 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000169 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000170 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 break;
172 case Function::LinkOnceLinkage:
173 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000174 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << '\n';
177 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000179 O << "\t.globl\t" << CurrentFnName << "\n"
180 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000182 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 }
184 break;
185 }
186 if (F->hasHiddenVisibility()) {
187 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 } else if (F->hasProtectedVisibility()) {
190 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000191 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 }
193
194 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000195 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 else if (Subtarget->isTargetCygMing()) {
197 O << "\t.def\t " << CurrentFnName
198 << ";\t.scl\t" <<
199 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
200 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
201 << ";\t.endef\n";
202 }
203
204 O << CurrentFnName << ":\n";
205 // Add some workaround for linkonce linkage on Cygwin\MinGW
206 if (Subtarget->isTargetCygMing() &&
207 (F->getLinkage() == Function::LinkOnceLinkage ||
208 F->getLinkage() == Function::WeakLinkage))
209 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000210}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212/// runOnMachineFunction - This uses the printMachineInstruction()
213/// method to print assembly for each instruction.
214///
215bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
216 const Function *F = MF.getFunction();
217 unsigned CC = F->getCallingConv();
218
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000219 SetupMachineFunction(MF);
220 O << "\n\n";
221
222 // Populate function information map. Actually, We don't want to populate
223 // non-stdcall or non-fastcall functions' information right now.
224 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
225 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
226
227 // Print out constants referenced by the function
228 EmitConstantPool(MF.getConstantPool());
229
230 if (F->hasDLLExportLinkage())
231 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
232
233 // Print the 'header' of function
234 emitFunctionHeader(MF);
235
236 // Emit pre-function debug and/or EH information.
237 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
238 DW.BeginFunction(&MF);
239
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000241 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
243 I != E; ++I) {
244 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000245 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000246 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 O << '\n';
248 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000249 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
250 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000252 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000253 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 printMachineInstruction(II);
255 }
256 }
257
Dale Johannesenf35771f2008-04-08 00:37:56 +0000258 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
259 // If the function is empty, then we need to emit *something*. Otherwise,
260 // the function's label might be associated with something that it wasn't
261 // meant to be associated with. We emit a noop in this situation.
262 // We are assuming inline asms are code.
263 O << "\tnop\n";
264 }
265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000267 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000269 // Emit post-function debug information.
270 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
273 // Print out jump tables referenced by the function.
274 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 // We didn't modify anything.
277 return false;
278}
279
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000280static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
282}
283
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000284static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
285 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
286 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
287}
288
289static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
291}
292
293void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
294 const char *Modifier, bool NotRIPRel) {
295 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 switch (MO.getType()) {
297 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000298 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 "Virtual registers should not make it this far!");
300 O << '%';
301 unsigned Reg = MO.getReg();
302 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000303 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
305 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
306 Reg = getX86SubSuperRegister(Reg, VT);
307 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000308 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 return;
310 }
311
312 case MachineOperand::MO_Immediate:
313 if (!Modifier ||
314 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
315 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000316 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 return;
318 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000319 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 return;
321 case MachineOperand::MO_JumpTableIndex: {
322 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
323 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000324 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000325 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
327 if (TM.getRelocationModel() == Reloc::PIC_) {
328 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000329 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
330 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 else if (Subtarget->isPICStyleGOT())
332 O << "@GOTOFF";
333 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
336 O << "(%rip)";
337 return;
338 }
339 case MachineOperand::MO_ConstantPoolIndex: {
340 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
341 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000342 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000343 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
345 if (TM.getRelocationModel() == Reloc::PIC_) {
346 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000347 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
348 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 else if (Subtarget->isPICStyleGOT())
350 O << "@GOTOFF";
351 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 int Offset = MO.getOffset();
354 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000355 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 else if (Offset < 0)
357 O << Offset;
358
359 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
360 O << "(%rip)";
361 return;
362 }
363 case MachineOperand::MO_GlobalAddress: {
364 bool isCallOp = Modifier && !strcmp(Modifier, "call");
365 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
366 bool needCloseParen = false;
367
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000368 const GlobalValue *GV = MO.getGlobal();
369 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
370 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000371 // If GV is an alias then use the aliasee for determining
372 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000373 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
374 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
375 }
376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 bool isThreadLocal = GVar && GVar->isThreadLocal();
378
379 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000380 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000381
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 if (!isMemOp && !isCallOp)
383 O << '$';
384 else if (Name[0] == '$') {
385 // The name begins with a dollar-sign. In order to avoid having it look
386 // like an integer immediate to the assembler, enclose it in parens.
387 O << '(';
388 needCloseParen = true;
389 }
390
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000391 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 // Link-once, declaration, or Weakly-linked global variables need
393 // non-lazily-resolved stubs
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000394 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 // Dynamically-resolved functions need a stub for the function.
396 if (isCallOp && isa<Function>(GV)) {
397 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000398 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 } else {
400 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000401 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 }
403 } else {
404 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000405 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 O << Name;
407 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000408
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000410 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 } else {
412 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000413 O << "__imp_";
414 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 O << Name;
416
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000417 if (isCallOp) {
418 if (shouldPrintPLT(TM, Subtarget)) {
419 // Assemble call via PLT for externally visible symbols
420 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
421 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 O << "@PLT";
423 }
424 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
425 // Save function name for later type emission
426 FnStubs.insert(Name);
427 }
428 }
429
430 if (GV->hasExternalWeakLinkage())
431 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000432
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 int Offset = MO.getOffset();
434 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000435 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 else if (Offset < 0)
437 O << Offset;
438
439 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000440 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 O << "@TLSGD"; // general dynamic TLS model
442 else
443 if (GV->isDeclaration())
444 O << "@INDNTPOFF"; // initial exec TLS model
445 else
446 O << "@NTPOFF"; // local exec TLS model
447 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000448 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
450 O << "@GOT";
451 else
452 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000453 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
454 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000455 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 O << "@GOTPCREL";
457
458 if (needCloseParen) {
459 needCloseParen = false;
460 O << ')';
461 }
462
463 // Use rip when possible to reduce code size, except when
464 // index or base register are also part of the address. e.g.
465 // foo(%rip)(%rcx,%rax,4) is not legal
466 O << "(%rip)";
467 }
468 }
469
470 if (needCloseParen)
471 O << ')';
472
473 return;
474 }
475 case MachineOperand::MO_ExternalSymbol: {
476 bool isCallOp = Modifier && !strcmp(Modifier, "call");
477 bool needCloseParen = false;
478 std::string Name(TAI->getGlobalPrefix());
479 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000480 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000482 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 return;
484 }
485 if (!isCallOp)
486 O << '$';
487 else if (Name[0] == '$') {
488 // The name begins with a dollar-sign. In order to avoid having it look
489 // like an integer immediate to the assembler, enclose it in parens.
490 O << '(';
491 needCloseParen = true;
492 }
493
494 O << Name;
495
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000496 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 std::string GOTName(TAI->getGlobalPrefix());
498 GOTName+="_GLOBAL_OFFSET_TABLE_";
499 if (Name == GOTName)
500 // HACK! Emit extra offset to PC during printing GOT offset to
501 // compensate for the size of popl instruction. The resulting code
502 // should look like:
503 // call .piclabel
504 // piclabel:
505 // popl %some_register
506 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
507 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000508 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509
510 if (isCallOp)
511 O << "@PLT";
512 }
513
514 if (needCloseParen)
515 O << ')';
516
517 if (!isCallOp && Subtarget->isPICStyleRIPRel())
518 O << "(%rip)";
519
520 return;
521 }
522 default:
523 O << "<unknown operand type>"; return;
524 }
525}
526
527void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000528 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 assert(value <= 7 && "Invalid ssecc argument!");
530 switch (value) {
531 case 0: O << "eq"; break;
532 case 1: O << "lt"; break;
533 case 2: O << "le"; break;
534 case 3: O << "unord"; break;
535 case 4: O << "neq"; break;
536 case 5: O << "nlt"; break;
537 case 6: O << "nle"; break;
538 case 7: O << "ord"; break;
539 }
540}
541
542void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
543 const char *Modifier){
544 assert(isMem(MI, Op) && "Invalid memory reference!");
545 MachineOperand BaseReg = MI->getOperand(Op);
546 MachineOperand IndexReg = MI->getOperand(Op+2);
547 const MachineOperand &DispSpec = MI->getOperand(Op+3);
548
549 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
550 if (DispSpec.isGlobalAddress() ||
551 DispSpec.isConstantPoolIndex() ||
552 DispSpec.isJumpTableIndex()) {
553 printOperand(MI, Op+3, "mem", NotRIPRel);
554 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000555 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
557 O << DispVal;
558 }
559
560 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000561 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000563
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 // There are cases where we can end up with ESP/RSP in the indexreg slot.
565 // If this happens, swap the base/index register to support assemblers that
566 // don't work when the index is *SP.
567 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
568 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
569 std::swap(BaseReg, IndexReg);
570 std::swap(BaseRegOperand, IndexRegOperand);
571 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000572
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000573 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 if (BaseReg.getReg())
575 printOperand(MI, Op+BaseRegOperand, Modifier);
576
577 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000578 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 printOperand(MI, Op+IndexRegOperand, Modifier);
580 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000581 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000583 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 }
585}
586
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000587void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000588 const MachineBasicBlock *MBB) const {
589 if (!TAI->getSetDirective())
590 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000591
592 // We don't need .set machinery if we have GOT-style relocations
593 if (Subtarget->isPICStyleGOT())
594 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000595
Evan Cheng6fb06762007-11-09 01:32:10 +0000596 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
597 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000598 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000599 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000600 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000601 << '_' << uid << '\n';
602 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000603 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000604}
605
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000607 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000608 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609}
610
611
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000612void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
613 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000614 unsigned uid) const
615{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000616 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
617 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
618
619 O << JTEntryDirective << ' ';
620
621 if (TM.getRelocationModel() == Reloc::PIC_) {
622 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
623 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
624 << '_' << uid << "_set_" << MBB->getNumber();
625 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000626 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000627 O << "@GOTOFF";
628 } else
629 assert(0 && "Don't know how to print MBB label for this PIC mode");
630 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000631 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000632}
633
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000634bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 unsigned Reg = MO.getReg();
637 switch (Mode) {
638 default: return true; // Unknown mode.
639 case 'b': // Print QImode register
640 Reg = getX86SubSuperRegister(Reg, MVT::i8);
641 break;
642 case 'h': // Print QImode high register
643 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
644 break;
645 case 'w': // Print HImode register
646 Reg = getX86SubSuperRegister(Reg, MVT::i16);
647 break;
648 case 'k': // Print SImode register
649 Reg = getX86SubSuperRegister(Reg, MVT::i32);
650 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000651 case 'q': // Print DImode register
652 Reg = getX86SubSuperRegister(Reg, MVT::i64);
653 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 }
655
Evan Cheng00d04a72008-07-07 22:21:06 +0000656 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 return false;
658}
659
660/// PrintAsmOperand - Print out an operand for an inline asm expression.
661///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000662bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000663 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 const char *ExtraCode) {
665 // Does this asm operand have a single letter operand modifier?
666 if (ExtraCode && ExtraCode[0]) {
667 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000668
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 switch (ExtraCode[0]) {
670 default: return true; // Unknown modifier.
671 case 'c': // Don't print "$" before a global var name or constant.
672 printOperand(MI, OpNo, "mem");
673 return false;
674 case 'b': // Print QImode register
675 case 'h': // Print QImode high register
676 case 'w': // Print HImode register
677 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000678 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000679 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
681 printOperand(MI, OpNo);
682 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000683
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 case 'P': // Don't print @PLT, but do print as memory.
685 printOperand(MI, OpNo, "mem");
686 return false;
687 }
688 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000689
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 printOperand(MI, OpNo);
691 return false;
692}
693
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000694bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000696 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000698 if (ExtraCode && ExtraCode[0]) {
699 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000700
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000701 switch (ExtraCode[0]) {
702 default: return true; // Unknown modifier.
703 case 'b': // Print QImode register
704 case 'h': // Print QImode high register
705 case 'w': // Print HImode register
706 case 'k': // Print SImode register
707 case 'q': // Print SImode register
708 // These only apply to registers, ignore on mem.
709 break;
710 }
711 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 printMemReference(MI, OpNo);
713 return false;
714}
715
716/// printMachineInstruction -- Print out a single X86 LLVM instruction
717/// MI in AT&T syntax to the current output stream.
718///
719void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
720 ++EmittedInsts;
721
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 // Call the autogenerated instruction printer routines.
723 printInstruction(MI);
724}
725
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000726/// doInitialization
727bool X86ATTAsmPrinter::doInitialization(Module &M) {
728 if (TAI->doesSupportDebugInformation()) {
729 // Emit initial debug information.
730 DW.BeginModule(&M);
731 }
732
Evan Cheng5cda7762008-07-09 06:36:53 +0000733 bool Result = AsmPrinter::doInitialization(M);
734
Dale Johannesen06545922008-07-09 20:55:35 +0000735 if (TAI->doesSupportDebugInformation()) {
736 // Let PassManager know we need debug information and relay
737 // the MachineModuleInfo address on to DwarfWriter.
738 // AsmPrinter::doInitialization did this analysis.
739 MMI = getAnalysisToUpdate<MachineModuleInfo>();
740 DW.SetModuleInfo(MMI);
741 }
742
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000743 // Darwin wants symbols to be quoted if they have complex names.
744 if (Subtarget->isTargetDarwin())
745 Mang->setUseQuotes(true);
746
747 return Result;
748}
749
750
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000751void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000752 const TargetData *TD = TM.getTargetData();
753
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000754 if (!GVar->hasInitializer())
755 return; // External global require no code
756
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000757 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikovb81503c2008-07-09 13:24:38 +0000758
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000759 // Check to see if this is a special global used by LLVM, if so, emit it.
760 if (EmitSpecialLLVMGlobal(GVar)) {
761 if (Subtarget->isTargetDarwin() &&
762 TM.getRelocationModel() == Reloc::Static) {
763 if (GVar->getName() == "llvm.global_ctors")
764 O << ".reference .constructors_used\n";
765 else if (GVar->getName() == "llvm.global_dtors")
766 O << ".reference .destructors_used\n";
767 }
768 return;
769 }
770
771 std::string name = Mang->getValueName(GVar);
772 Constant *C = GVar->getInitializer();
773 const Type *Type = C->getType();
774 unsigned Size = TD->getABITypeSize(Type);
775 unsigned Align = TD->getPreferredAlignmentLog(GVar);
776
777 if (GVar->hasHiddenVisibility()) {
778 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000779 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000780 } else if (GVar->hasProtectedVisibility()) {
781 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000782 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000783 }
784
785 if (Subtarget->isTargetELF())
786 O << "\t.type\t" << name << ",@object\n";
787
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000788 SwitchToDataSection(SectionName.c_str());
789
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000790 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000791 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000792 if (GVar->hasExternalLinkage()) {
793 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000794 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000796 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000797 return;
798 }
799 }
800
801 if (!GVar->isThreadLocal() &&
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000802 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000803 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000804
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805 if (TAI->getLCOMMDirective() != NULL) {
806 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000811 O << "\t.globl " << name << '\n'
812 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000816 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000817 EmitGlobalConstant(C);
818 return;
819 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000820 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821
822 // Leopard and above support aligned common symbols.
823 if (Subtarget->getDarwinVers() >= 9)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 }
826 } else {
827 if (!Subtarget->isTargetCygMing()) {
828 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000829 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000831 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000836 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000837 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 return;
839 }
840 }
841
842 switch (GVar->getLinkage()) {
843 case GlobalValue::CommonLinkage:
844 case GlobalValue::LinkOnceLinkage:
845 case GlobalValue::WeakLinkage:
846 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000847 O << "\t.globl " << name << '\n'
848 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000851 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000853 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 }
855 break;
856 case GlobalValue::DLLExportLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 case GlobalValue::AppendingLinkage:
858 // FIXME: appending linkage variables should go into a section of
859 // their name or something. For now, just emit them as external.
860 case GlobalValue::ExternalLinkage:
861 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 // FALL THROUGH
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000864 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 break;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 default:
867 assert(0 && "Unknown linkage type!");
868 }
869
870 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000871 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000873 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000874 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000875 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000876
877 // If the initializer is a extern weak symbol, remember to emit the weak
878 // reference!
879 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
880 if (GV->hasExternalWeakLinkage())
881 ExtWeakSymbols.insert(GV);
882
883 EmitGlobalConstant(C);
884}
885
Evan Cheng76443dc2008-07-08 00:55:58 +0000886/// printGVStub - Print stub for a global value.
887///
888void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000889 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000890 O << ":\n\t.indirect_symbol ";
891 if (Prefix) O << Prefix;
892 O << GV << "\n\t.long\t0\n";
893}
894
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000895
896bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000897 // Print out module-level global variables here.
898 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000899 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000900 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000901
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000902 if (I->hasDLLExportLinkage())
903 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
904 }
905
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000906 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000907 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000908 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909
910 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
911 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000912 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000913 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000914
915 if (!DLLExportedFns.empty()) {
916 SwitchToDataSection(".section .drectve");
917 }
918
919 for (StringSet<>::iterator i = DLLExportedFns.begin(),
920 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000921 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000922 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000923
924 if (Subtarget->isTargetDarwin()) {
925 SwitchToDataSection("");
926
927 // Output stubs for dynamically-linked functions
928 unsigned j = 1;
929 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
930 i != e; ++i, ++j) {
931 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
932 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000933 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000934 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000935 O << ":\n"
936 "\t.indirect_symbol " << p << "\n"
937 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000938 }
939
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000940 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000941
Evan Cheng76443dc2008-07-08 00:55:58 +0000942 // Print global value stubs.
943 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000944 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
945 // Add the (possibly multiple) personalities to the set of global values.
946 // Only referenced functions get into the Personalities list.
947 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000948 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000949 E = Personalities.end(); I != E; ++I) {
950 if (!*I)
951 continue;
952 if (!InStubSection) {
953 SwitchToDataSection(
954 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
955 InStubSection = true;
956 }
957 printGVStub((*I)->getNameStart(), "_");
958 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000959 }
960
961 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000962 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000963 SwitchToDataSection(
964 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
965 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000966 i != e; ++i)
967 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000968
969 // Emit final debug information.
970 DW.EndModule();
971
972 // Funny Darwin hack: This flag tells the linker that no global symbols
973 // contain code that falls through to other global symbols (e.g. the obvious
974 // implementation of multiple entry points). If this doesn't occur, the
975 // linker can safely perform dead code stripping. Since LLVM never
976 // generates code that does this, it is always safe to set.
977 O << "\t.subsections_via_symbols\n";
978 } else if (Subtarget->isTargetCygMing()) {
979 // Emit type information for external functions
980 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
981 i != e; ++i) {
982 O << "\t.def\t " << i->getKeyData()
983 << ";\t.scl\t" << COFF::C_EXT
984 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
985 << ";\t.endef\n";
986 }
987
988 // Emit final debug information.
989 DW.EndModule();
990 } else if (Subtarget->isTargetELF()) {
991 // Emit final debug information.
992 DW.EndModule();
993 }
994
995 return AsmPrinter::doFinalization(M);
996}
997
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998// Include the auto-generated portion of the assembly writer.
999#include "X86GenAsmWriter.inc"