blob: bb519560a184853793ecb7c77beabfe1e8264d15 [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 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000186
187 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188
189 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000190 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 else if (Subtarget->isTargetCygMing()) {
192 O << "\t.def\t " << CurrentFnName
193 << ";\t.scl\t" <<
194 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
195 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
196 << ";\t.endef\n";
197 }
198
199 O << CurrentFnName << ":\n";
200 // Add some workaround for linkonce linkage on Cygwin\MinGW
201 if (Subtarget->isTargetCygMing() &&
202 (F->getLinkage() == Function::LinkOnceLinkage ||
203 F->getLinkage() == Function::WeakLinkage))
204 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000205}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000207/// runOnMachineFunction - This uses the printMachineInstruction()
208/// method to print assembly for each instruction.
209///
210bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
211 const Function *F = MF.getFunction();
212 unsigned CC = F->getCallingConv();
213
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000214 SetupMachineFunction(MF);
215 O << "\n\n";
216
217 // Populate function information map. Actually, We don't want to populate
218 // non-stdcall or non-fastcall functions' information right now.
219 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
220 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
221
222 // Print out constants referenced by the function
223 EmitConstantPool(MF.getConstantPool());
224
225 if (F->hasDLLExportLinkage())
226 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
227
228 // Print the 'header' of function
229 emitFunctionHeader(MF);
230
231 // Emit pre-function debug and/or EH information.
232 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
233 DW.BeginFunction(&MF);
234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000236 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
238 I != E; ++I) {
239 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000240 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000241 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 O << '\n';
243 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000244 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
245 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000247 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000248 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 printMachineInstruction(II);
250 }
251 }
252
Dale Johannesenf35771f2008-04-08 00:37:56 +0000253 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
254 // If the function is empty, then we need to emit *something*. Otherwise,
255 // the function's label might be associated with something that it wasn't
256 // meant to be associated with. We emit a noop in this situation.
257 // We are assuming inline asms are code.
258 O << "\tnop\n";
259 }
260
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000262 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000264 // Emit post-function debug information.
265 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267
268 // Print out jump tables referenced by the function.
269 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000270
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 // We didn't modify anything.
272 return false;
273}
274
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000275static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
277}
278
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000279static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
280 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
281 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
282}
283
284static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
286}
287
288void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
289 const char *Modifier, bool NotRIPRel) {
290 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 switch (MO.getType()) {
292 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000293 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 "Virtual registers should not make it this far!");
295 O << '%';
296 unsigned Reg = MO.getReg();
297 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000298 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
300 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
301 Reg = getX86SubSuperRegister(Reg, VT);
302 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000303 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 return;
305 }
306
307 case MachineOperand::MO_Immediate:
308 if (!Modifier ||
309 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
310 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000311 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 return;
313 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000314 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 return;
316 case MachineOperand::MO_JumpTableIndex: {
317 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
318 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000319 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000320 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
322 if (TM.getRelocationModel() == Reloc::PIC_) {
323 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000324 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
325 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 else if (Subtarget->isPICStyleGOT())
327 O << "@GOTOFF";
328 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000329
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
331 O << "(%rip)";
332 return;
333 }
334 case MachineOperand::MO_ConstantPoolIndex: {
335 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
336 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000337 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000338 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
340 if (TM.getRelocationModel() == Reloc::PIC_) {
341 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000342 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
343 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 else if (Subtarget->isPICStyleGOT())
345 O << "@GOTOFF";
346 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 int Offset = MO.getOffset();
349 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000350 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 else if (Offset < 0)
352 O << Offset;
353
354 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
355 O << "(%rip)";
356 return;
357 }
358 case MachineOperand::MO_GlobalAddress: {
359 bool isCallOp = Modifier && !strcmp(Modifier, "call");
360 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
361 bool needCloseParen = false;
362
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000363 const GlobalValue *GV = MO.getGlobal();
364 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
365 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000366 // If GV is an alias then use the aliasee for determining
367 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000368 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
369 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
370 }
371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 bool isThreadLocal = GVar && GVar->isThreadLocal();
373
374 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000375 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 if (!isMemOp && !isCallOp)
378 O << '$';
379 else if (Name[0] == '$') {
380 // The name begins with a dollar-sign. In order to avoid having it look
381 // like an integer immediate to the assembler, enclose it in parens.
382 O << '(';
383 needCloseParen = true;
384 }
385
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000386 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 // Link-once, declaration, or Weakly-linked global variables need
388 // non-lazily-resolved stubs
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000389 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 // Dynamically-resolved functions need a stub for the function.
391 if (isCallOp && isa<Function>(GV)) {
392 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000393 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 } else {
395 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000396 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 }
398 } else {
399 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000400 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 O << Name;
402 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000403
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000405 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 } else {
407 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000408 O << "__imp_";
409 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 O << Name;
411
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000412 if (isCallOp) {
413 if (shouldPrintPLT(TM, Subtarget)) {
414 // Assemble call via PLT for externally visible symbols
415 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
416 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 O << "@PLT";
418 }
419 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
420 // Save function name for later type emission
421 FnStubs.insert(Name);
422 }
423 }
424
425 if (GV->hasExternalWeakLinkage())
426 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000427
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 int Offset = MO.getOffset();
429 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000430 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 else if (Offset < 0)
432 O << Offset;
433
434 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000435 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 O << "@TLSGD"; // general dynamic TLS model
437 else
438 if (GV->isDeclaration())
439 O << "@INDNTPOFF"; // initial exec TLS model
440 else
441 O << "@NTPOFF"; // local exec TLS model
442 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000443 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
445 O << "@GOT";
446 else
447 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000448 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
449 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000450 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 O << "@GOTPCREL";
452
453 if (needCloseParen) {
454 needCloseParen = false;
455 O << ')';
456 }
457
458 // Use rip when possible to reduce code size, except when
459 // index or base register are also part of the address. e.g.
460 // foo(%rip)(%rcx,%rax,4) is not legal
461 O << "(%rip)";
462 }
463 }
464
465 if (needCloseParen)
466 O << ')';
467
468 return;
469 }
470 case MachineOperand::MO_ExternalSymbol: {
471 bool isCallOp = Modifier && !strcmp(Modifier, "call");
472 bool needCloseParen = false;
473 std::string Name(TAI->getGlobalPrefix());
474 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000475 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000477 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 return;
479 }
480 if (!isCallOp)
481 O << '$';
482 else if (Name[0] == '$') {
483 // The name begins with a dollar-sign. In order to avoid having it look
484 // like an integer immediate to the assembler, enclose it in parens.
485 O << '(';
486 needCloseParen = true;
487 }
488
489 O << Name;
490
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000491 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 std::string GOTName(TAI->getGlobalPrefix());
493 GOTName+="_GLOBAL_OFFSET_TABLE_";
494 if (Name == GOTName)
495 // HACK! Emit extra offset to PC during printing GOT offset to
496 // compensate for the size of popl instruction. The resulting code
497 // should look like:
498 // call .piclabel
499 // piclabel:
500 // popl %some_register
501 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
502 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000503 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504
505 if (isCallOp)
506 O << "@PLT";
507 }
508
509 if (needCloseParen)
510 O << ')';
511
512 if (!isCallOp && Subtarget->isPICStyleRIPRel())
513 O << "(%rip)";
514
515 return;
516 }
517 default:
518 O << "<unknown operand type>"; return;
519 }
520}
521
522void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000523 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 assert(value <= 7 && "Invalid ssecc argument!");
525 switch (value) {
526 case 0: O << "eq"; break;
527 case 1: O << "lt"; break;
528 case 2: O << "le"; break;
529 case 3: O << "unord"; break;
530 case 4: O << "neq"; break;
531 case 5: O << "nlt"; break;
532 case 6: O << "nle"; break;
533 case 7: O << "ord"; break;
534 }
535}
536
537void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
538 const char *Modifier){
539 assert(isMem(MI, Op) && "Invalid memory reference!");
540 MachineOperand BaseReg = MI->getOperand(Op);
541 MachineOperand IndexReg = MI->getOperand(Op+2);
542 const MachineOperand &DispSpec = MI->getOperand(Op+3);
543
544 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
545 if (DispSpec.isGlobalAddress() ||
546 DispSpec.isConstantPoolIndex() ||
547 DispSpec.isJumpTableIndex()) {
548 printOperand(MI, Op+3, "mem", NotRIPRel);
549 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000550 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
552 O << DispVal;
553 }
554
555 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000556 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000558
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 // There are cases where we can end up with ESP/RSP in the indexreg slot.
560 // If this happens, swap the base/index register to support assemblers that
561 // don't work when the index is *SP.
562 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
563 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
564 std::swap(BaseReg, IndexReg);
565 std::swap(BaseRegOperand, IndexRegOperand);
566 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000567
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000568 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 if (BaseReg.getReg())
570 printOperand(MI, Op+BaseRegOperand, Modifier);
571
572 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000573 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 printOperand(MI, Op+IndexRegOperand, Modifier);
575 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000576 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000578 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 }
580}
581
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000582void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000583 const MachineBasicBlock *MBB) const {
584 if (!TAI->getSetDirective())
585 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000586
587 // We don't need .set machinery if we have GOT-style relocations
588 if (Subtarget->isPICStyleGOT())
589 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000590
Evan Cheng6fb06762007-11-09 01:32:10 +0000591 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
592 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000593 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000594 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000595 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000596 << '_' << uid << '\n';
597 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000598 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000599}
600
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000602 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000603 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604}
605
606
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000607void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
608 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000609 unsigned uid) const
610{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000611 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
612 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
613
614 O << JTEntryDirective << ' ';
615
616 if (TM.getRelocationModel() == Reloc::PIC_) {
617 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
618 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
619 << '_' << uid << "_set_" << MBB->getNumber();
620 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000621 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000622 O << "@GOTOFF";
623 } else
624 assert(0 && "Don't know how to print MBB label for this PIC mode");
625 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000626 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000627}
628
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000629bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 unsigned Reg = MO.getReg();
632 switch (Mode) {
633 default: return true; // Unknown mode.
634 case 'b': // Print QImode register
635 Reg = getX86SubSuperRegister(Reg, MVT::i8);
636 break;
637 case 'h': // Print QImode high register
638 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
639 break;
640 case 'w': // Print HImode register
641 Reg = getX86SubSuperRegister(Reg, MVT::i16);
642 break;
643 case 'k': // Print SImode register
644 Reg = getX86SubSuperRegister(Reg, MVT::i32);
645 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000646 case 'q': // Print DImode register
647 Reg = getX86SubSuperRegister(Reg, MVT::i64);
648 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 }
650
Evan Cheng00d04a72008-07-07 22:21:06 +0000651 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 return false;
653}
654
655/// PrintAsmOperand - Print out an operand for an inline asm expression.
656///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000657bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000658 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 const char *ExtraCode) {
660 // Does this asm operand have a single letter operand modifier?
661 if (ExtraCode && ExtraCode[0]) {
662 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 switch (ExtraCode[0]) {
665 default: return true; // Unknown modifier.
666 case 'c': // Don't print "$" before a global var name or constant.
667 printOperand(MI, OpNo, "mem");
668 return false;
669 case 'b': // Print QImode register
670 case 'h': // Print QImode high register
671 case 'w': // Print HImode register
672 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000673 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000674 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
676 printOperand(MI, OpNo);
677 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000678
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 case 'P': // Don't print @PLT, but do print as memory.
680 printOperand(MI, OpNo, "mem");
681 return false;
682 }
683 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000684
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 printOperand(MI, OpNo);
686 return false;
687}
688
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000689bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000691 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000693 if (ExtraCode && ExtraCode[0]) {
694 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000695
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000696 switch (ExtraCode[0]) {
697 default: return true; // Unknown modifier.
698 case 'b': // Print QImode register
699 case 'h': // Print QImode high register
700 case 'w': // Print HImode register
701 case 'k': // Print SImode register
702 case 'q': // Print SImode register
703 // These only apply to registers, ignore on mem.
704 break;
705 }
706 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 printMemReference(MI, OpNo);
708 return false;
709}
710
711/// printMachineInstruction -- Print out a single X86 LLVM instruction
712/// MI in AT&T syntax to the current output stream.
713///
714void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
715 ++EmittedInsts;
716
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 // Call the autogenerated instruction printer routines.
718 printInstruction(MI);
719}
720
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000721/// doInitialization
722bool X86ATTAsmPrinter::doInitialization(Module &M) {
723 if (TAI->doesSupportDebugInformation()) {
724 // Emit initial debug information.
725 DW.BeginModule(&M);
726 }
727
Evan Cheng5cda7762008-07-09 06:36:53 +0000728 bool Result = AsmPrinter::doInitialization(M);
729
Dale Johannesen06545922008-07-09 20:55:35 +0000730 if (TAI->doesSupportDebugInformation()) {
731 // Let PassManager know we need debug information and relay
732 // the MachineModuleInfo address on to DwarfWriter.
733 // AsmPrinter::doInitialization did this analysis.
734 MMI = getAnalysisToUpdate<MachineModuleInfo>();
735 DW.SetModuleInfo(MMI);
736 }
737
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000738 // Darwin wants symbols to be quoted if they have complex names.
739 if (Subtarget->isTargetDarwin())
740 Mang->setUseQuotes(true);
741
742 return Result;
743}
744
745
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000746void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000747 const TargetData *TD = TM.getTargetData();
748
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000749 if (!GVar->hasInitializer())
750 return; // External global require no code
751
752 // Check to see if this is a special global used by LLVM, if so, emit it.
753 if (EmitSpecialLLVMGlobal(GVar)) {
754 if (Subtarget->isTargetDarwin() &&
755 TM.getRelocationModel() == Reloc::Static) {
756 if (GVar->getName() == "llvm.global_ctors")
757 O << ".reference .constructors_used\n";
758 else if (GVar->getName() == "llvm.global_dtors")
759 O << ".reference .destructors_used\n";
760 }
761 return;
762 }
763
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000764 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000765 std::string name = Mang->getValueName(GVar);
766 Constant *C = GVar->getInitializer();
767 const Type *Type = C->getType();
768 unsigned Size = TD->getABITypeSize(Type);
769 unsigned Align = TD->getPreferredAlignmentLog(GVar);
770
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000771 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000772
773 if (Subtarget->isTargetELF())
774 O << "\t.type\t" << name << ",@object\n";
775
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000776 SwitchToDataSection(SectionName.c_str());
777
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000778 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000779 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000780 if (GVar->hasExternalLinkage()) {
781 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000782 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000783 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000784 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000785 return;
786 }
787 }
788
789 if (!GVar->isThreadLocal() &&
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000790 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000791 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000792
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000793 if (TAI->getLCOMMDirective() != NULL) {
794 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000795 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000796 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000797 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000798 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000799 O << "\t.globl " << name << '\n'
800 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000801 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000802 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000803 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000804 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805 EmitGlobalConstant(C);
806 return;
807 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000808 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000809
810 // Leopard and above support aligned common symbols.
811 if (Subtarget->getDarwinVers() >= 9)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 }
814 } else {
815 if (!Subtarget->isTargetCygMing()) {
816 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000817 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000818 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000819 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000821 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000822 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000823 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000824 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000825 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 return;
827 }
828 }
829
830 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000831 case GlobalValue::CommonLinkage:
832 case GlobalValue::LinkOnceLinkage:
833 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << "\t.globl " << name << '\n'
836 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000837 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000839 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000840 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000841 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000842 }
843 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000844 case GlobalValue::DLLExportLinkage:
845 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000846 // FIXME: appending linkage variables should go into a section of
847 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000848 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000850 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000851 // FALL THROUGH
Evan Cheng630c5612008-08-08 06:43:59 +0000852 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000853 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000854 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 assert(0 && "Unknown linkage type!");
856 }
857
858 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000859 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000860 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000861 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000862 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000863 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000864
865 // If the initializer is a extern weak symbol, remember to emit the weak
866 // reference!
867 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
868 if (GV->hasExternalWeakLinkage())
869 ExtWeakSymbols.insert(GV);
870
871 EmitGlobalConstant(C);
872}
873
Evan Cheng76443dc2008-07-08 00:55:58 +0000874/// printGVStub - Print stub for a global value.
875///
876void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000877 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000878 O << ":\n\t.indirect_symbol ";
879 if (Prefix) O << Prefix;
880 O << GV << "\n\t.long\t0\n";
881}
882
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883
884bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000885 // Print out module-level global variables here.
886 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000887 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000888 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000889
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000890 if (I->hasDLLExportLinkage())
891 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
892 }
893
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000894 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000895 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000896 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000897
898 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
899 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000900 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000901 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000902
903 if (!DLLExportedFns.empty()) {
904 SwitchToDataSection(".section .drectve");
905 }
906
907 for (StringSet<>::iterator i = DLLExportedFns.begin(),
908 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000909 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000910 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000911
912 if (Subtarget->isTargetDarwin()) {
913 SwitchToDataSection("");
914
915 // Output stubs for dynamically-linked functions
916 unsigned j = 1;
917 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
918 i != e; ++i, ++j) {
919 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
920 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000921 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000922 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000923 O << ":\n"
924 "\t.indirect_symbol " << p << "\n"
925 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000926 }
927
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000928 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000929
Evan Cheng76443dc2008-07-08 00:55:58 +0000930 // Print global value stubs.
931 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000932 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
933 // Add the (possibly multiple) personalities to the set of global values.
934 // Only referenced functions get into the Personalities list.
935 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000936 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000937 E = Personalities.end(); I != E; ++I) {
938 if (!*I)
939 continue;
940 if (!InStubSection) {
941 SwitchToDataSection(
942 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
943 InStubSection = true;
944 }
945 printGVStub((*I)->getNameStart(), "_");
946 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000947 }
948
949 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000950 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000951 SwitchToDataSection(
952 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
953 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000954 i != e; ++i)
955 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000956
957 // Emit final debug information.
958 DW.EndModule();
959
960 // Funny Darwin hack: This flag tells the linker that no global symbols
961 // contain code that falls through to other global symbols (e.g. the obvious
962 // implementation of multiple entry points). If this doesn't occur, the
963 // linker can safely perform dead code stripping. Since LLVM never
964 // generates code that does this, it is always safe to set.
965 O << "\t.subsections_via_symbols\n";
966 } else if (Subtarget->isTargetCygMing()) {
967 // Emit type information for external functions
968 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
969 i != e; ++i) {
970 O << "\t.def\t " << i->getKeyData()
971 << ";\t.scl\t" << COFF::C_EXT
972 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
973 << ";\t.endef\n";
974 }
975
976 // Emit final debug information.
977 DW.EndModule();
978 } else if (Subtarget->isTargetELF()) {
979 // Emit final debug information.
980 DW.EndModule();
981 }
982
983 return AsmPrinter::doFinalization(M);
984}
985
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986// Include the auto-generated portion of the assembly writer.
987#include "X86GenAsmWriter.inc"