blob: 61c8f12eec36222ab6466440449e814d4fe51dcf [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetAsmInfo.h"
33#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
36STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
Evan Cheng0729ccf2008-01-05 00:41:47 +000038static std::string getPICLabelString(unsigned FnNum,
39 const TargetAsmInfo *TAI,
40 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 std::string label;
42 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000043 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000045 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 else
47 assert(0 && "Don't know how to print PIC label!\n");
48
49 return label;
50}
51
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000052static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
53 const TargetData *TD) {
54 X86MachineFunctionInfo Info;
55 uint64_t Size = 0;
56
57 switch (F->getCallingConv()) {
58 case CallingConv::X86_StdCall:
59 Info.setDecorationStyle(StdCall);
60 break;
61 case CallingConv::X86_FastCall:
62 Info.setDecorationStyle(FastCall);
63 break;
64 default:
65 return Info;
66 }
67
68 unsigned argNum = 1;
69 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
70 AI != AE; ++AI, ++argNum) {
71 const Type* Ty = AI->getType();
72
73 // 'Dereference' type in case of byval parameter attribute
74 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
75 Ty = cast<PointerType>(Ty)->getElementType();
76
77 // Size should be aligned to DWORD boundary
78 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
79 }
80
81 // We're not supporting tooooo huge arguments :)
82 Info.setBytesToPopOnReturn((unsigned int)Size);
83 return Info;
84}
85
86/// PrintUnmangledNameSafely - Print out the printable characters in the name.
87/// Don't print things like \n or \0.
Owen Anderson847b99b2008-08-21 00:14:44 +000088static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000089 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
90 Name != E; ++Name)
91 if (isprint(*Name))
92 OS << *Name;
93}
94
95/// decorateName - Query FunctionInfoMap and use this information for various
96/// name decoration.
97void X86ATTAsmPrinter::decorateName(std::string &Name,
98 const GlobalValue *GV) {
99 const Function *F = dyn_cast<Function>(GV);
100 if (!F) return;
101
102 // We don't want to decorate non-stdcall or non-fastcall functions right now
103 unsigned CC = F->getCallingConv();
104 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
105 return;
106
107 // Decorate names only when we're targeting Cygwin/Mingw32 targets
108 if (!Subtarget->isTargetCygMing())
109 return;
110
111 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
112
113 const X86MachineFunctionInfo *Info;
114 if (info_item == FunctionInfoMap.end()) {
115 // Calculate apropriate function info and populate map
116 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
117 Info = &FunctionInfoMap[F];
118 } else {
119 Info = &info_item->second;
120 }
121
122 const FunctionType *FT = F->getFunctionType();
123 switch (Info->getDecorationStyle()) {
124 case None:
125 break;
126 case StdCall:
127 // "Pure" variadic functions do not receive @0 suffix.
128 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
129 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
130 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
131 break;
132 case FastCall:
133 // "Pure" variadic functions do not receive @0 suffix.
134 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
135 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
137
138 if (Name[0] == '_') {
139 Name[0] = '@';
140 } else {
141 Name = '@' + Name;
142 }
143 break;
144 default:
145 assert(0 && "Unsupported DecorationStyle");
146 }
147}
148
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000149void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 const Function *F = MF.getFunction();
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000151 std::string SectionName = TAI->SectionForGlobal(F);
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 Korobeynikov8b967b52008-07-09 13:28:19 +0000155 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000156
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000157 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Devang Pateldc1611f2008-09-24 00:06:15 +0000158 if (!F->isDeclaration() && F->hasNote(FnAttr::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.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000163 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 break;
165 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000167 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000168 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 break;
170 case Function::LinkOnceLinkage:
171 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000174 O << "\t.globl\t" << CurrentFnName << '\n';
175 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << "\n"
178 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182 break;
183 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000184
185 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000188 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 else if (Subtarget->isTargetCygMing()) {
190 O << "\t.def\t " << CurrentFnName
191 << ";\t.scl\t" <<
192 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
193 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
194 << ";\t.endef\n";
195 }
196
197 O << CurrentFnName << ":\n";
198 // Add some workaround for linkonce linkage on Cygwin\MinGW
199 if (Subtarget->isTargetCygMing() &&
200 (F->getLinkage() == Function::LinkOnceLinkage ||
201 F->getLinkage() == Function::WeakLinkage))
202 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000203}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000205/// runOnMachineFunction - This uses the printMachineInstruction()
206/// method to print assembly for each instruction.
207///
208bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
209 const Function *F = MF.getFunction();
210 unsigned CC = F->getCallingConv();
211
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212 SetupMachineFunction(MF);
213 O << "\n\n";
214
215 // Populate function information map. Actually, We don't want to populate
216 // non-stdcall or non-fastcall functions' information right now.
217 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
218 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
219
220 // Print out constants referenced by the function
221 EmitConstantPool(MF.getConstantPool());
222
223 if (F->hasDLLExportLinkage())
224 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
225
226 // Print the 'header' of function
227 emitFunctionHeader(MF);
228
229 // Emit pre-function debug and/or EH information.
230 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
231 DW.BeginFunction(&MF);
232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000234 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
236 I != E; ++I) {
237 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000238 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000239 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 O << '\n';
241 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000242 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
243 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000245 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000246 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 printMachineInstruction(II);
248 }
249 }
250
Dale Johannesenf35771f2008-04-08 00:37:56 +0000251 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
252 // If the function is empty, then we need to emit *something*. Otherwise,
253 // the function's label might be associated with something that it wasn't
254 // meant to be associated with. We emit a noop in this situation.
255 // We are assuming inline asms are code.
256 O << "\tnop\n";
257 }
258
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000260 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000262 // Emit post-function debug information.
263 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265
266 // Print out jump tables referenced by the function.
267 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // We didn't modify anything.
270 return false;
271}
272
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000273static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
275}
276
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000277static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
278 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
279 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
280}
281
282static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
284}
285
286void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
287 const char *Modifier, bool NotRIPRel) {
288 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 switch (MO.getType()) {
290 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000291 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 "Virtual registers should not make it this far!");
293 O << '%';
294 unsigned Reg = MO.getReg();
295 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000296 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
298 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
299 Reg = getX86SubSuperRegister(Reg, VT);
300 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000301 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 return;
303 }
304
305 case MachineOperand::MO_Immediate:
306 if (!Modifier ||
307 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
308 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000309 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 return;
311 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000312 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 return;
314 case MachineOperand::MO_JumpTableIndex: {
315 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
316 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000317 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000318 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
320 if (TM.getRelocationModel() == Reloc::PIC_) {
321 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000322 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
323 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 else if (Subtarget->isPICStyleGOT())
325 O << "@GOTOFF";
326 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000327
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
329 O << "(%rip)";
330 return;
331 }
332 case MachineOperand::MO_ConstantPoolIndex: {
333 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
334 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000335 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000336 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337
338 if (TM.getRelocationModel() == Reloc::PIC_) {
339 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000340 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
341 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 else if (Subtarget->isPICStyleGOT())
343 O << "@GOTOFF";
344 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000345
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 int Offset = MO.getOffset();
347 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000348 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 else if (Offset < 0)
350 O << Offset;
351
352 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
353 O << "(%rip)";
354 return;
355 }
356 case MachineOperand::MO_GlobalAddress: {
357 bool isCallOp = Modifier && !strcmp(Modifier, "call");
358 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
359 bool needCloseParen = false;
360
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000361 const GlobalValue *GV = MO.getGlobal();
362 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
363 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000364 // If GV is an alias then use the aliasee for determining
365 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000366 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000367 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000368 }
369
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 bool isThreadLocal = GVar && GVar->isThreadLocal();
371
372 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000373 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 if (!isMemOp && !isCallOp)
376 O << '$';
377 else if (Name[0] == '$') {
378 // The name begins with a dollar-sign. In order to avoid having it look
379 // like an integer immediate to the assembler, enclose it in parens.
380 O << '(';
381 needCloseParen = true;
382 }
383
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000384 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 // Link-once, declaration, or Weakly-linked global variables need
386 // non-lazily-resolved stubs
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000387 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 // Dynamically-resolved functions need a stub for the function.
389 if (isCallOp && isa<Function>(GV)) {
Evan Chengdfd884e2008-09-20 00:13:45 +0000390 // Function stubs are no longer needed for Mac OS X 10.5 and up.
391 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
392 O << Name;
393 } else {
394 FnStubs.insert(Name);
395 printSuffixedName(Name, "$stub");
396 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 } else {
398 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000399 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 }
401 } else {
402 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000403 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 O << Name;
405 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000406
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000408 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 } else {
410 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000411 O << "__imp_";
412 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 O << Name;
414
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000415 if (isCallOp) {
416 if (shouldPrintPLT(TM, Subtarget)) {
417 // Assemble call via PLT for externally visible symbols
418 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
419 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 O << "@PLT";
421 }
422 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
423 // Save function name for later type emission
424 FnStubs.insert(Name);
425 }
426 }
427
428 if (GV->hasExternalWeakLinkage())
429 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000430
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 int Offset = MO.getOffset();
432 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000433 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 else if (Offset < 0)
435 O << Offset;
436
437 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000438 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 O << "@TLSGD"; // general dynamic TLS model
440 else
441 if (GV->isDeclaration())
442 O << "@INDNTPOFF"; // initial exec TLS model
443 else
444 O << "@NTPOFF"; // local exec TLS model
445 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000446 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
448 O << "@GOT";
449 else
450 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000451 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
452 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000453 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 O << "@GOTPCREL";
455
456 if (needCloseParen) {
457 needCloseParen = false;
458 O << ')';
459 }
460
461 // Use rip when possible to reduce code size, except when
462 // index or base register are also part of the address. e.g.
463 // foo(%rip)(%rcx,%rax,4) is not legal
464 O << "(%rip)";
465 }
466 }
467
468 if (needCloseParen)
469 O << ')';
470
471 return;
472 }
473 case MachineOperand::MO_ExternalSymbol: {
474 bool isCallOp = Modifier && !strcmp(Modifier, "call");
475 bool needCloseParen = false;
476 std::string Name(TAI->getGlobalPrefix());
477 Name += MO.getSymbolName();
Evan Chengdfd884e2008-09-20 00:13:45 +0000478 // Print function stub suffix unless it's Mac OS X 10.5 and up.
479 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
480 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
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
757 // Check to see if this is a special global used by LLVM, if so, emit it.
758 if (EmitSpecialLLVMGlobal(GVar)) {
759 if (Subtarget->isTargetDarwin() &&
760 TM.getRelocationModel() == Reloc::Static) {
761 if (GVar->getName() == "llvm.global_ctors")
762 O << ".reference .constructors_used\n";
763 else if (GVar->getName() == "llvm.global_dtors")
764 O << ".reference .destructors_used\n";
765 }
766 return;
767 }
768
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000769 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000770 std::string name = Mang->getValueName(GVar);
771 Constant *C = GVar->getInitializer();
772 const Type *Type = C->getType();
773 unsigned Size = TD->getABITypeSize(Type);
774 unsigned Align = TD->getPreferredAlignmentLog(GVar);
775
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000776 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000777
778 if (Subtarget->isTargetELF())
779 O << "\t.type\t" << name << ",@object\n";
780
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000781 SwitchToDataSection(SectionName.c_str());
782
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000783 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000784 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000785 if (GVar->hasExternalLinkage()) {
786 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000787 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000788 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000789 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000790 return;
791 }
792 }
793
794 if (!GVar->isThreadLocal() &&
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000795 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000796 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000797
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000798 if (TAI->getLCOMMDirective() != NULL) {
799 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000800 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000801 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000802 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000803 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000804 O << "\t.globl " << name << '\n'
805 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 EmitGlobalConstant(C);
811 return;
812 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000813 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000814 if (TAI->getCOMMDirectiveTakesAlignment())
815 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000816 }
817 } else {
818 if (!Subtarget->isTargetCygMing()) {
819 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000820 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000826 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000827 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000828 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000829 return;
830 }
831 }
832
833 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000834 case GlobalValue::CommonLinkage:
835 case GlobalValue::LinkOnceLinkage:
836 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000837 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << "\t.globl " << name << '\n'
839 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000840 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000842 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000844 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 }
846 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000847 case GlobalValue::DLLExportLinkage:
848 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849 // FIXME: appending linkage variables should go into a section of
850 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000851 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000853 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 // FALL THROUGH
Evan Cheng630c5612008-08-08 06:43:59 +0000855 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000856 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000857 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000858 assert(0 && "Unknown linkage type!");
859 }
860
861 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000864 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000866 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867
868 // If the initializer is a extern weak symbol, remember to emit the weak
869 // reference!
870 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
871 if (GV->hasExternalWeakLinkage())
872 ExtWeakSymbols.insert(GV);
873
874 EmitGlobalConstant(C);
875}
876
Evan Cheng76443dc2008-07-08 00:55:58 +0000877/// printGVStub - Print stub for a global value.
878///
879void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000880 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000881 O << ":\n\t.indirect_symbol ";
882 if (Prefix) O << Prefix;
883 O << GV << "\n\t.long\t0\n";
884}
885
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000886
887bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000888 // Print out module-level global variables here.
889 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000890 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000891 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000892
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000893 if (I->hasDLLExportLinkage())
894 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
895 }
896
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000897 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000898 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000899 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000900
901 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
902 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000903 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000904 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000905
906 if (!DLLExportedFns.empty()) {
907 SwitchToDataSection(".section .drectve");
908 }
909
910 for (StringSet<>::iterator i = DLLExportedFns.begin(),
911 e = DLLExportedFns.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() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000914
915 if (Subtarget->isTargetDarwin()) {
916 SwitchToDataSection("");
917
918 // Output stubs for dynamically-linked functions
919 unsigned j = 1;
920 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
921 i != e; ++i, ++j) {
922 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
923 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000924 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000925 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000926 O << ":\n"
927 "\t.indirect_symbol " << p << "\n"
928 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000929 }
930
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000931 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000932
Evan Cheng76443dc2008-07-08 00:55:58 +0000933 // Print global value stubs.
934 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000935 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
936 // Add the (possibly multiple) personalities to the set of global values.
937 // Only referenced functions get into the Personalities list.
938 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000939 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000940 E = Personalities.end(); I != E; ++I) {
941 if (!*I)
942 continue;
943 if (!InStubSection) {
944 SwitchToDataSection(
945 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
946 InStubSection = true;
947 }
948 printGVStub((*I)->getNameStart(), "_");
949 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000950 }
951
952 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000953 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000954 SwitchToDataSection(
955 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
956 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000957 i != e; ++i)
958 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000959
960 // Emit final debug information.
961 DW.EndModule();
962
963 // Funny Darwin hack: This flag tells the linker that no global symbols
964 // contain code that falls through to other global symbols (e.g. the obvious
965 // implementation of multiple entry points). If this doesn't occur, the
966 // linker can safely perform dead code stripping. Since LLVM never
967 // generates code that does this, it is always safe to set.
968 O << "\t.subsections_via_symbols\n";
969 } else if (Subtarget->isTargetCygMing()) {
970 // Emit type information for external functions
971 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
972 i != e; ++i) {
973 O << "\t.def\t " << i->getKeyData()
974 << ";\t.scl\t" << COFF::C_EXT
975 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
976 << ";\t.endef\n";
977 }
978
979 // Emit final debug information.
980 DW.EndModule();
981 } else if (Subtarget->isTargetELF()) {
982 // Emit final debug information.
983 DW.EndModule();
984 }
985
986 return AsmPrinter::doFinalization(M);
987}
988
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989// Include the auto-generated portion of the assembly writer.
990#include "X86GenAsmWriter.inc"