blob: 4a9002c13b4c9084fbd6040c37d6492702c7f1fa [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
Anton Korobeynikovb81503c2008-07-09 13:24:38 +000035#include <iostream>
36
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Evan Cheng0729ccf2008-01-05 00:41:47 +000039static std::string getPICLabelString(unsigned FnNum,
40 const TargetAsmInfo *TAI,
41 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 std::string label;
43 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000044 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000046 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 else
48 assert(0 && "Don't know how to print PIC label!\n");
49
50 return label;
51}
52
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000053static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
54 const TargetData *TD) {
55 X86MachineFunctionInfo Info;
56 uint64_t Size = 0;
57
58 switch (F->getCallingConv()) {
59 case CallingConv::X86_StdCall:
60 Info.setDecorationStyle(StdCall);
61 break;
62 case CallingConv::X86_FastCall:
63 Info.setDecorationStyle(FastCall);
64 break;
65 default:
66 return Info;
67 }
68
69 unsigned argNum = 1;
70 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
71 AI != AE; ++AI, ++argNum) {
72 const Type* Ty = AI->getType();
73
74 // 'Dereference' type in case of byval parameter attribute
75 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
76 Ty = cast<PointerType>(Ty)->getElementType();
77
78 // Size should be aligned to DWORD boundary
79 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
80 }
81
82 // We're not supporting tooooo huge arguments :)
83 Info.setBytesToPopOnReturn((unsigned int)Size);
84 return Info;
85}
86
87/// PrintUnmangledNameSafely - Print out the printable characters in the name.
88/// Don't print things like \n or \0.
89static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
90 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
91 Name != E; ++Name)
92 if (isprint(*Name))
93 OS << *Name;
94}
95
96/// decorateName - Query FunctionInfoMap and use this information for various
97/// name decoration.
98void X86ATTAsmPrinter::decorateName(std::string &Name,
99 const GlobalValue *GV) {
100 const Function *F = dyn_cast<Function>(GV);
101 if (!F) return;
102
103 // We don't want to decorate non-stdcall or non-fastcall functions right now
104 unsigned CC = F->getCallingConv();
105 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
106 return;
107
108 // Decorate names only when we're targeting Cygwin/Mingw32 targets
109 if (!Subtarget->isTargetCygMing())
110 return;
111
112 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
113
114 const X86MachineFunctionInfo *Info;
115 if (info_item == FunctionInfoMap.end()) {
116 // Calculate apropriate function info and populate map
117 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
118 Info = &FunctionInfoMap[F];
119 } else {
120 Info = &info_item->second;
121 }
122
123 const FunctionType *FT = F->getFunctionType();
124 switch (Info->getDecorationStyle()) {
125 case None:
126 break;
127 case StdCall:
128 // "Pure" variadic functions do not receive @0 suffix.
129 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
131 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
132 break;
133 case FastCall:
134 // "Pure" variadic functions do not receive @0 suffix.
135 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
137 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
138
139 if (Name[0] == '_') {
140 Name[0] = '@';
141 } else {
142 Name = '@' + Name;
143 }
144 break;
145 default:
146 assert(0 && "Unsupported DecorationStyle");
147 }
148}
149
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150/// getSectionForFunction - Return the section that we should emit the
151/// specified function body into.
152std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
153 switch (F.getLinkage()) {
154 default: assert(0 && "Unknown linkage type!");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000155 case Function::InternalLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 case Function::DLLExportLinkage:
157 case Function::ExternalLinkage:
158 return TAI->getTextSection();
159 case Function::WeakLinkage:
160 case Function::LinkOnceLinkage:
161 if (Subtarget->isTargetDarwin()) {
162 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
163 } else if (Subtarget->isTargetCygMing()) {
164 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
165 } else {
166 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
167 ",\"ax\",@progbits";
168 }
169 }
170}
171
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000172void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000175 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
177 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000178
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000179 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 switch (F->getLinkage()) {
181 default: assert(0 && "Unknown linkage type!");
182 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000183 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 break;
185 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000187 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 break;
190 case Function::LinkOnceLinkage:
191 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000192 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000194 O << "\t.globl\t" << CurrentFnName << '\n';
195 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000197 O << "\t.globl\t" << CurrentFnName << "\n"
198 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000200 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 }
202 break;
203 }
204 if (F->hasHiddenVisibility()) {
205 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000206 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 } else if (F->hasProtectedVisibility()) {
208 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000209 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 }
211
212 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000213 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 else if (Subtarget->isTargetCygMing()) {
215 O << "\t.def\t " << CurrentFnName
216 << ";\t.scl\t" <<
217 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
218 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
219 << ";\t.endef\n";
220 }
221
222 O << CurrentFnName << ":\n";
223 // Add some workaround for linkonce linkage on Cygwin\MinGW
224 if (Subtarget->isTargetCygMing() &&
225 (F->getLinkage() == Function::LinkOnceLinkage ||
226 F->getLinkage() == Function::WeakLinkage))
227 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000228}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000230/// runOnMachineFunction - This uses the printMachineInstruction()
231/// method to print assembly for each instruction.
232///
233bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
234 const Function *F = MF.getFunction();
235 unsigned CC = F->getCallingConv();
236
Evan Cheng5cda7762008-07-09 06:36:53 +0000237 if (TAI->doesSupportDebugInformation()) {
238 // Let PassManager know we need debug information and relay
239 // the MachineModuleInfo address on to DwarfWriter.
240 MMI = &getAnalysis<MachineModuleInfo>();
241 DW.SetModuleInfo(MMI);
242 }
243
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000244 SetupMachineFunction(MF);
245 O << "\n\n";
246
247 // Populate function information map. Actually, We don't want to populate
248 // non-stdcall or non-fastcall functions' information right now.
249 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
250 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
251
252 // Print out constants referenced by the function
253 EmitConstantPool(MF.getConstantPool());
254
255 if (F->hasDLLExportLinkage())
256 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
257
258 // Print the 'header' of function
259 emitFunctionHeader(MF);
260
261 // Emit pre-function debug and/or EH information.
262 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
263 DW.BeginFunction(&MF);
264
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000266 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
268 I != E; ++I) {
269 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000270 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000271 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 O << '\n';
273 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000274 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
275 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000277 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000278 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 printMachineInstruction(II);
280 }
281 }
282
Dale Johannesenf35771f2008-04-08 00:37:56 +0000283 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
284 // If the function is empty, then we need to emit *something*. Otherwise,
285 // the function's label might be associated with something that it wasn't
286 // meant to be associated with. We emit a noop in this situation.
287 // We are assuming inline asms are code.
288 O << "\tnop\n";
289 }
290
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000292 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000294 // Emit post-function debug information.
295 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
298 // Print out jump tables referenced by the function.
299 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 // We didn't modify anything.
302 return false;
303}
304
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000305static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
307}
308
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000309static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
310 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
311 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
312}
313
314static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
316}
317
318void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
319 const char *Modifier, bool NotRIPRel) {
320 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 switch (MO.getType()) {
322 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000323 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 "Virtual registers should not make it this far!");
325 O << '%';
326 unsigned Reg = MO.getReg();
327 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000328 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
330 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
331 Reg = getX86SubSuperRegister(Reg, VT);
332 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000333 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 return;
335 }
336
337 case MachineOperand::MO_Immediate:
338 if (!Modifier ||
339 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
340 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000341 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 return;
343 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000344 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 return;
346 case MachineOperand::MO_JumpTableIndex: {
347 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
348 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000349 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000350 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351
352 if (TM.getRelocationModel() == Reloc::PIC_) {
353 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000354 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
355 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 else if (Subtarget->isPICStyleGOT())
357 O << "@GOTOFF";
358 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000359
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
361 O << "(%rip)";
362 return;
363 }
364 case MachineOperand::MO_ConstantPoolIndex: {
365 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
366 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000367 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000368 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369
370 if (TM.getRelocationModel() == Reloc::PIC_) {
371 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000372 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
373 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 else if (Subtarget->isPICStyleGOT())
375 O << "@GOTOFF";
376 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000377
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 int Offset = MO.getOffset();
379 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000380 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 else if (Offset < 0)
382 O << Offset;
383
384 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
385 O << "(%rip)";
386 return;
387 }
388 case MachineOperand::MO_GlobalAddress: {
389 bool isCallOp = Modifier && !strcmp(Modifier, "call");
390 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
391 bool needCloseParen = false;
392
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000393 const GlobalValue *GV = MO.getGlobal();
394 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
395 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000396 // If GV is an alias then use the aliasee for determining
397 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000398 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
399 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
400 }
401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 bool isThreadLocal = GVar && GVar->isThreadLocal();
403
404 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000405 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000406
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 if (!isMemOp && !isCallOp)
408 O << '$';
409 else if (Name[0] == '$') {
410 // The name begins with a dollar-sign. In order to avoid having it look
411 // like an integer immediate to the assembler, enclose it in parens.
412 O << '(';
413 needCloseParen = true;
414 }
415
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000416 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 // Link-once, declaration, or Weakly-linked global variables need
418 // non-lazily-resolved stubs
419 if (GV->isDeclaration() ||
420 GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000421 GV->hasLinkOnceLinkage() ||
422 GV->hasCommonLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 // Dynamically-resolved functions need a stub for the function.
424 if (isCallOp && isa<Function>(GV)) {
425 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000426 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 } else {
428 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000429 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 }
431 } else {
432 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000433 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 O << Name;
435 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000436
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000438 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 } else {
440 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000441 O << "__imp_";
442 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 O << Name;
444
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000445 if (isCallOp) {
446 if (shouldPrintPLT(TM, Subtarget)) {
447 // Assemble call via PLT for externally visible symbols
448 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
449 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 O << "@PLT";
451 }
452 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
453 // Save function name for later type emission
454 FnStubs.insert(Name);
455 }
456 }
457
458 if (GV->hasExternalWeakLinkage())
459 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000460
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 int Offset = MO.getOffset();
462 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000463 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 else if (Offset < 0)
465 O << Offset;
466
467 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000468 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 O << "@TLSGD"; // general dynamic TLS model
470 else
471 if (GV->isDeclaration())
472 O << "@INDNTPOFF"; // initial exec TLS model
473 else
474 O << "@NTPOFF"; // local exec TLS model
475 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000476 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
478 O << "@GOT";
479 else
480 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000481 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
482 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000483 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 O << "@GOTPCREL";
485
486 if (needCloseParen) {
487 needCloseParen = false;
488 O << ')';
489 }
490
491 // Use rip when possible to reduce code size, except when
492 // index or base register are also part of the address. e.g.
493 // foo(%rip)(%rcx,%rax,4) is not legal
494 O << "(%rip)";
495 }
496 }
497
498 if (needCloseParen)
499 O << ')';
500
501 return;
502 }
503 case MachineOperand::MO_ExternalSymbol: {
504 bool isCallOp = Modifier && !strcmp(Modifier, "call");
505 bool needCloseParen = false;
506 std::string Name(TAI->getGlobalPrefix());
507 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000508 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000510 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 return;
512 }
513 if (!isCallOp)
514 O << '$';
515 else if (Name[0] == '$') {
516 // The name begins with a dollar-sign. In order to avoid having it look
517 // like an integer immediate to the assembler, enclose it in parens.
518 O << '(';
519 needCloseParen = true;
520 }
521
522 O << Name;
523
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000524 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 std::string GOTName(TAI->getGlobalPrefix());
526 GOTName+="_GLOBAL_OFFSET_TABLE_";
527 if (Name == GOTName)
528 // HACK! Emit extra offset to PC during printing GOT offset to
529 // compensate for the size of popl instruction. The resulting code
530 // should look like:
531 // call .piclabel
532 // piclabel:
533 // popl %some_register
534 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
535 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000536 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537
538 if (isCallOp)
539 O << "@PLT";
540 }
541
542 if (needCloseParen)
543 O << ')';
544
545 if (!isCallOp && Subtarget->isPICStyleRIPRel())
546 O << "(%rip)";
547
548 return;
549 }
550 default:
551 O << "<unknown operand type>"; return;
552 }
553}
554
555void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000556 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 assert(value <= 7 && "Invalid ssecc argument!");
558 switch (value) {
559 case 0: O << "eq"; break;
560 case 1: O << "lt"; break;
561 case 2: O << "le"; break;
562 case 3: O << "unord"; break;
563 case 4: O << "neq"; break;
564 case 5: O << "nlt"; break;
565 case 6: O << "nle"; break;
566 case 7: O << "ord"; break;
567 }
568}
569
570void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
571 const char *Modifier){
572 assert(isMem(MI, Op) && "Invalid memory reference!");
573 MachineOperand BaseReg = MI->getOperand(Op);
574 MachineOperand IndexReg = MI->getOperand(Op+2);
575 const MachineOperand &DispSpec = MI->getOperand(Op+3);
576
577 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
578 if (DispSpec.isGlobalAddress() ||
579 DispSpec.isConstantPoolIndex() ||
580 DispSpec.isJumpTableIndex()) {
581 printOperand(MI, Op+3, "mem", NotRIPRel);
582 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000583 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
585 O << DispVal;
586 }
587
588 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000589 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000591
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 // There are cases where we can end up with ESP/RSP in the indexreg slot.
593 // If this happens, swap the base/index register to support assemblers that
594 // don't work when the index is *SP.
595 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
596 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
597 std::swap(BaseReg, IndexReg);
598 std::swap(BaseRegOperand, IndexRegOperand);
599 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000600
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000601 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 if (BaseReg.getReg())
603 printOperand(MI, Op+BaseRegOperand, Modifier);
604
605 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000606 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 printOperand(MI, Op+IndexRegOperand, Modifier);
608 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000609 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000611 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
613}
614
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000615void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000616 const MachineBasicBlock *MBB) const {
617 if (!TAI->getSetDirective())
618 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000619
620 // We don't need .set machinery if we have GOT-style relocations
621 if (Subtarget->isPICStyleGOT())
622 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000623
Evan Cheng6fb06762007-11-09 01:32:10 +0000624 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
625 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000626 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000627 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000628 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000629 << '_' << uid << '\n';
630 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000631 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000632}
633
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000635 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000636 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637}
638
639
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000640void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
641 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000642 unsigned uid) const
643{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000644 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
645 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
646
647 O << JTEntryDirective << ' ';
648
649 if (TM.getRelocationModel() == Reloc::PIC_) {
650 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
651 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
652 << '_' << uid << "_set_" << MBB->getNumber();
653 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000654 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000655 O << "@GOTOFF";
656 } else
657 assert(0 && "Don't know how to print MBB label for this PIC mode");
658 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000659 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000660}
661
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000662bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 unsigned Reg = MO.getReg();
665 switch (Mode) {
666 default: return true; // Unknown mode.
667 case 'b': // Print QImode register
668 Reg = getX86SubSuperRegister(Reg, MVT::i8);
669 break;
670 case 'h': // Print QImode high register
671 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
672 break;
673 case 'w': // Print HImode register
674 Reg = getX86SubSuperRegister(Reg, MVT::i16);
675 break;
676 case 'k': // Print SImode register
677 Reg = getX86SubSuperRegister(Reg, MVT::i32);
678 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000679 case 'q': // Print DImode register
680 Reg = getX86SubSuperRegister(Reg, MVT::i64);
681 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 }
683
Evan Cheng00d04a72008-07-07 22:21:06 +0000684 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 return false;
686}
687
688/// PrintAsmOperand - Print out an operand for an inline asm expression.
689///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000690bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000691 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 const char *ExtraCode) {
693 // Does this asm operand have a single letter operand modifier?
694 if (ExtraCode && ExtraCode[0]) {
695 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000696
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 switch (ExtraCode[0]) {
698 default: return true; // Unknown modifier.
699 case 'c': // Don't print "$" before a global var name or constant.
700 printOperand(MI, OpNo, "mem");
701 return false;
702 case 'b': // Print QImode register
703 case 'h': // Print QImode high register
704 case 'w': // Print HImode register
705 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000706 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000707 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
709 printOperand(MI, OpNo);
710 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000711
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 case 'P': // Don't print @PLT, but do print as memory.
713 printOperand(MI, OpNo, "mem");
714 return false;
715 }
716 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000717
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 printOperand(MI, OpNo);
719 return false;
720}
721
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000722bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000724 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000726 if (ExtraCode && ExtraCode[0]) {
727 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000728
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000729 switch (ExtraCode[0]) {
730 default: return true; // Unknown modifier.
731 case 'b': // Print QImode register
732 case 'h': // Print QImode high register
733 case 'w': // Print HImode register
734 case 'k': // Print SImode register
735 case 'q': // Print SImode register
736 // These only apply to registers, ignore on mem.
737 break;
738 }
739 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 printMemReference(MI, OpNo);
741 return false;
742}
743
744/// printMachineInstruction -- Print out a single X86 LLVM instruction
745/// MI in AT&T syntax to the current output stream.
746///
747void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
748 ++EmittedInsts;
749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 // Call the autogenerated instruction printer routines.
751 printInstruction(MI);
752}
753
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000754/// doInitialization
755bool X86ATTAsmPrinter::doInitialization(Module &M) {
756 if (TAI->doesSupportDebugInformation()) {
757 // Emit initial debug information.
758 DW.BeginModule(&M);
759 }
760
Evan Cheng5cda7762008-07-09 06:36:53 +0000761 bool Result = AsmPrinter::doInitialization(M);
762
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000763 // Darwin wants symbols to be quoted if they have complex names.
764 if (Subtarget->isTargetDarwin())
765 Mang->setUseQuotes(true);
766
767 return Result;
768}
769
770
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000771void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000772 const TargetData *TD = TM.getTargetData();
773
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000774 if (!GVar->hasInitializer())
775 return; // External global require no code
776
Anton Korobeynikovb81503c2008-07-09 13:24:38 +0000777 GVar->dump();
778 std::cout << TAI->SectionForGlobal(GVar) << std::endl;
779
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000780 // Check to see if this is a special global used by LLVM, if so, emit it.
781 if (EmitSpecialLLVMGlobal(GVar)) {
782 if (Subtarget->isTargetDarwin() &&
783 TM.getRelocationModel() == Reloc::Static) {
784 if (GVar->getName() == "llvm.global_ctors")
785 O << ".reference .constructors_used\n";
786 else if (GVar->getName() == "llvm.global_dtors")
787 O << ".reference .destructors_used\n";
788 }
789 return;
790 }
791
792 std::string name = Mang->getValueName(GVar);
793 Constant *C = GVar->getInitializer();
794 const Type *Type = C->getType();
795 unsigned Size = TD->getABITypeSize(Type);
796 unsigned Align = TD->getPreferredAlignmentLog(GVar);
797
798 if (GVar->hasHiddenVisibility()) {
799 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000800 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000801 } else if (GVar->hasProtectedVisibility()) {
802 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000803 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000804 }
805
806 if (Subtarget->isTargetELF())
807 O << "\t.type\t" << name << ",@object\n";
808
809 if (C->isNullValue() && !GVar->hasSection()) {
810 if (GVar->hasExternalLinkage()) {
811 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 return;
816 }
817 }
818
819 if (!GVar->isThreadLocal() &&
820 (GVar->hasInternalLinkage() || GVar->hasWeakLinkage() ||
821 GVar->hasLinkOnceLinkage() || GVar->hasCommonLinkage())) {
822 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
823 if (!NoZerosInBSS && TAI->getBSSSection())
824 SwitchToDataSection(TAI->getBSSSection(), GVar);
825 else
826 SwitchToDataSection(TAI->getDataSection(), GVar);
827 if (TAI->getLCOMMDirective() != NULL) {
828 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000829 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000831 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << "\t.globl " << name << '\n'
834 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
836 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000837 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000839 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000840 EmitGlobalConstant(C);
841 return;
842 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000843 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000844
845 // Leopard and above support aligned common symbols.
846 if (Subtarget->getDarwinVers() >= 9)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000847 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848 }
849 } else {
850 if (!Subtarget->isTargetCygMing()) {
851 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000852 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000853 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000854 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000856 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000859 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000860 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 return;
862 }
863 }
864
865 switch (GVar->getLinkage()) {
866 case GlobalValue::CommonLinkage:
867 case GlobalValue::LinkOnceLinkage:
868 case GlobalValue::WeakLinkage:
869 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000870 O << "\t.globl " << name << '\n'
871 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872 if (!GVar->isConstant())
873 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
874 else {
875 const ArrayType *AT = dyn_cast<ArrayType>(Type);
876 if (AT && AT->getElementType()==Type::Int8Ty)
877 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", GVar);
878 else
879 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", GVar);
880 }
881 } else if (Subtarget->isTargetCygMing()) {
882 std::string SectionName(".section\t.data$linkonce." +
883 name +
884 ",\"aw\"");
885 SwitchToDataSection(SectionName.c_str(), GVar);
886 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000887 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000888 } else {
889 std::string SectionName("\t.section\t.llvm.linkonce.d." +
890 name +
891 ",\"aw\",@progbits");
892 SwitchToDataSection(SectionName.c_str(), GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000893 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000894 }
895 break;
896 case GlobalValue::DLLExportLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000897 case GlobalValue::AppendingLinkage:
898 // FIXME: appending linkage variables should go into a section of
899 // their name or something. For now, just emit them as external.
900 case GlobalValue::ExternalLinkage:
901 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000902 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000903 // FALL THROUGH
904 case GlobalValue::InternalLinkage: {
905 if (GVar->isConstant()) {
906 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
907 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
908 SwitchToDataSection(TAI->getCStringSection(), GVar);
909 break;
910 }
911 }
912 // FIXME: special handling for ".ctors" & ".dtors" sections
913 if (GVar->hasSection() &&
914 (GVar->getSection() == ".ctors" || GVar->getSection() == ".dtors")) {
915 std::string SectionName = ".section " + GVar->getSection();
916
917 if (Subtarget->isTargetCygMing()) {
918 SectionName += ",\"aw\"";
919 } else {
920 assert(!Subtarget->isTargetDarwin());
921 SectionName += ",\"aw\",@progbits";
922 }
923 SwitchToDataSection(SectionName.c_str());
924 } else if (GVar->hasSection() && Subtarget->isTargetDarwin()) {
925 // Honor all section names on Darwin; ObjC uses this
926 std::string SectionName = ".section " + GVar->getSection();
927 SwitchToDataSection(SectionName.c_str());
928 } else {
929 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
930 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSBSSSection() :
931 TAI->getBSSSection(), GVar);
932 else if (!GVar->isConstant())
933 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSDataSection() :
934 TAI->getDataSection(), GVar);
935 else if (GVar->isThreadLocal())
936 SwitchToDataSection(TAI->getTLSDataSection());
937 else {
938 // Read-only data.
939 bool HasReloc = C->ContainsRelocations();
940 if (HasReloc &&
941 Subtarget->isTargetDarwin() &&
942 TM.getRelocationModel() != Reloc::Static)
943 SwitchToDataSection("\t.const_data\n");
944 else if (!HasReloc && Size == 4 &&
945 TAI->getFourByteConstantSection())
946 SwitchToDataSection(TAI->getFourByteConstantSection(), GVar);
947 else if (!HasReloc && Size == 8 &&
948 TAI->getEightByteConstantSection())
949 SwitchToDataSection(TAI->getEightByteConstantSection(), GVar);
950 else if (!HasReloc && Size == 16 &&
951 TAI->getSixteenByteConstantSection())
952 SwitchToDataSection(TAI->getSixteenByteConstantSection(), GVar);
953 else if (TAI->getReadOnlySection())
954 SwitchToDataSection(TAI->getReadOnlySection(), GVar);
955 else
956 SwitchToDataSection(TAI->getDataSection(), GVar);
957 }
958 }
959
960 break;
961 }
962 default:
963 assert(0 && "Unknown linkage type!");
964 }
965
966 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000967 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000968 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000969 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000970 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000971 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000972
973 // If the initializer is a extern weak symbol, remember to emit the weak
974 // reference!
975 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
976 if (GV->hasExternalWeakLinkage())
977 ExtWeakSymbols.insert(GV);
978
979 EmitGlobalConstant(C);
980}
981
Evan Cheng76443dc2008-07-08 00:55:58 +0000982/// printGVStub - Print stub for a global value.
983///
984void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000985 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000986 O << ":\n\t.indirect_symbol ";
987 if (Prefix) O << Prefix;
988 O << GV << "\n\t.long\t0\n";
989}
990
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000991
992bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000993 // Print out module-level global variables here.
994 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000995 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000996 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000997
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000998 if (I->hasDLLExportLinkage())
999 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
1000 }
1001
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001002 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001003 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001004 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001005
1006 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1007 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001008 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001009 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001010
1011 if (!DLLExportedFns.empty()) {
1012 SwitchToDataSection(".section .drectve");
1013 }
1014
1015 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1016 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001017 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001018 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001019
1020 if (Subtarget->isTargetDarwin()) {
1021 SwitchToDataSection("");
1022
1023 // Output stubs for dynamically-linked functions
1024 unsigned j = 1;
1025 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1026 i != e; ++i, ++j) {
1027 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1028 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +00001029 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001030 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001031 O << ":\n"
1032 "\t.indirect_symbol " << p << "\n"
1033 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001034 }
1035
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001036 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001037
Evan Cheng76443dc2008-07-08 00:55:58 +00001038 // Print global value stubs.
1039 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001040 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1041 // Add the (possibly multiple) personalities to the set of global values.
1042 // Only referenced functions get into the Personalities list.
1043 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001044 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +00001045 E = Personalities.end(); I != E; ++I) {
1046 if (!*I)
1047 continue;
1048 if (!InStubSection) {
1049 SwitchToDataSection(
1050 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1051 InStubSection = true;
1052 }
1053 printGVStub((*I)->getNameStart(), "_");
1054 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001055 }
1056
1057 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +00001058 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001059 SwitchToDataSection(
1060 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1061 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +00001062 i != e; ++i)
1063 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001064
1065 // Emit final debug information.
1066 DW.EndModule();
1067
1068 // Funny Darwin hack: This flag tells the linker that no global symbols
1069 // contain code that falls through to other global symbols (e.g. the obvious
1070 // implementation of multiple entry points). If this doesn't occur, the
1071 // linker can safely perform dead code stripping. Since LLVM never
1072 // generates code that does this, it is always safe to set.
1073 O << "\t.subsections_via_symbols\n";
1074 } else if (Subtarget->isTargetCygMing()) {
1075 // Emit type information for external functions
1076 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1077 i != e; ++i) {
1078 O << "\t.def\t " << i->getKeyData()
1079 << ";\t.scl\t" << COFF::C_EXT
1080 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1081 << ";\t.endef\n";
1082 }
1083
1084 // Emit final debug information.
1085 DW.EndModule();
1086 } else if (Subtarget->isTargetELF()) {
1087 // Emit final debug information.
1088 DW.EndModule();
1089 }
1090
1091 return AsmPrinter::doFinalization(M);
1092}
1093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094// Include the auto-generated portion of the assembly writer.
1095#include "X86GenAsmWriter.inc"