blob: 6e83cbc5915a626d0d4790c5814d044ea60fd2a9 [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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148/// getSectionForFunction - Return the section that we should emit the
149/// specified function body into.
150std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
151 switch (F.getLinkage()) {
152 default: assert(0 && "Unknown linkage type!");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000153 case Function::InternalLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 case Function::DLLExportLinkage:
155 case Function::ExternalLinkage:
156 return TAI->getTextSection();
157 case Function::WeakLinkage:
158 case Function::LinkOnceLinkage:
159 if (Subtarget->isTargetDarwin()) {
160 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
161 } else if (Subtarget->isTargetCygMing()) {
162 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
163 } else {
164 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
165 ",\"ax\",@progbits";
166 }
167 }
168}
169
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000170void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000173 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174
175 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000176
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000177 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 switch (F->getLinkage()) {
179 default: assert(0 && "Unknown linkage type!");
180 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000181 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 break;
183 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000185 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000186 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 break;
188 case Function::LinkOnceLinkage:
189 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000190 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000192 O << "\t.globl\t" << CurrentFnName << '\n';
193 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000195 O << "\t.globl\t" << CurrentFnName << "\n"
196 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000198 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 }
200 break;
201 }
202 if (F->hasHiddenVisibility()) {
203 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000204 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 } else if (F->hasProtectedVisibility()) {
206 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000207 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 }
209
210 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000211 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 else if (Subtarget->isTargetCygMing()) {
213 O << "\t.def\t " << CurrentFnName
214 << ";\t.scl\t" <<
215 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
216 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
217 << ";\t.endef\n";
218 }
219
220 O << CurrentFnName << ":\n";
221 // Add some workaround for linkonce linkage on Cygwin\MinGW
222 if (Subtarget->isTargetCygMing() &&
223 (F->getLinkage() == Function::LinkOnceLinkage ||
224 F->getLinkage() == Function::WeakLinkage))
225 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000226}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000228/// runOnMachineFunction - This uses the printMachineInstruction()
229/// method to print assembly for each instruction.
230///
231bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
232 const Function *F = MF.getFunction();
233 unsigned CC = F->getCallingConv();
234
235 if (TAI->doesSupportDebugInformation()) {
236 // Let PassManager know we need debug information and relay
237 // the MachineModuleInfo address on to DwarfWriter.
238 MMI = &getAnalysis<MachineModuleInfo>();
239 DW.SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 }
241
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000242 SetupMachineFunction(MF);
243 O << "\n\n";
244
245 // Populate function information map. Actually, We don't want to populate
246 // non-stdcall or non-fastcall functions' information right now.
247 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
248 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
249
250 // Print out constants referenced by the function
251 EmitConstantPool(MF.getConstantPool());
252
253 if (F->hasDLLExportLinkage())
254 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
255
256 // Print the 'header' of function
257 emitFunctionHeader(MF);
258
259 // Emit pre-function debug and/or EH information.
260 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
261 DW.BeginFunction(&MF);
262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000264 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
266 I != E; ++I) {
267 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000268 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000269 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 O << '\n';
271 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000272 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
273 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000275 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000276 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 printMachineInstruction(II);
278 }
279 }
280
Dale Johannesenf35771f2008-04-08 00:37:56 +0000281 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
282 // If the function is empty, then we need to emit *something*. Otherwise,
283 // the function's label might be associated with something that it wasn't
284 // meant to be associated with. We emit a noop in this situation.
285 // We are assuming inline asms are code.
286 O << "\tnop\n";
287 }
288
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000290 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000292 // Emit post-function debug information.
293 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295
296 // Print out jump tables referenced by the function.
297 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000298
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 // We didn't modify anything.
300 return false;
301}
302
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000303static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
305}
306
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000307static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
308 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
309 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
310}
311
312static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
314}
315
316void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
317 const char *Modifier, bool NotRIPRel) {
318 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 switch (MO.getType()) {
320 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000321 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 "Virtual registers should not make it this far!");
323 O << '%';
324 unsigned Reg = MO.getReg();
325 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000326 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
328 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
329 Reg = getX86SubSuperRegister(Reg, VT);
330 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000331 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 return;
333 }
334
335 case MachineOperand::MO_Immediate:
336 if (!Modifier ||
337 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
338 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000339 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 return;
341 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000342 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 return;
344 case MachineOperand::MO_JumpTableIndex: {
345 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
346 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000347 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000348 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349
350 if (TM.getRelocationModel() == Reloc::PIC_) {
351 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000352 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
353 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 else if (Subtarget->isPICStyleGOT())
355 O << "@GOTOFF";
356 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
359 O << "(%rip)";
360 return;
361 }
362 case MachineOperand::MO_ConstantPoolIndex: {
363 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
364 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000365 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000366 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367
368 if (TM.getRelocationModel() == Reloc::PIC_) {
369 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000370 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
371 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 else if (Subtarget->isPICStyleGOT())
373 O << "@GOTOFF";
374 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 int Offset = MO.getOffset();
377 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000378 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 else if (Offset < 0)
380 O << Offset;
381
382 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
383 O << "(%rip)";
384 return;
385 }
386 case MachineOperand::MO_GlobalAddress: {
387 bool isCallOp = Modifier && !strcmp(Modifier, "call");
388 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
389 bool needCloseParen = false;
390
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000391 const GlobalValue *GV = MO.getGlobal();
392 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
393 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000394 // If GV is an alias then use the aliasee for determining
395 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000396 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
397 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
398 }
399
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 bool isThreadLocal = GVar && GVar->isThreadLocal();
401
402 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000403 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000404
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 if (!isMemOp && !isCallOp)
406 O << '$';
407 else if (Name[0] == '$') {
408 // The name begins with a dollar-sign. In order to avoid having it look
409 // like an integer immediate to the assembler, enclose it in parens.
410 O << '(';
411 needCloseParen = true;
412 }
413
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000414 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 // Link-once, declaration, or Weakly-linked global variables need
416 // non-lazily-resolved stubs
417 if (GV->isDeclaration() ||
418 GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000419 GV->hasLinkOnceLinkage() ||
420 GV->hasCommonLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 // Dynamically-resolved functions need a stub for the function.
422 if (isCallOp && isa<Function>(GV)) {
423 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000424 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 } else {
426 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000427 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 }
429 } else {
430 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000431 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 O << Name;
433 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000434
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000436 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 } else {
438 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000439 O << "__imp_";
440 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 O << Name;
442
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000443 if (isCallOp) {
444 if (shouldPrintPLT(TM, Subtarget)) {
445 // Assemble call via PLT for externally visible symbols
446 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
447 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 O << "@PLT";
449 }
450 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
451 // Save function name for later type emission
452 FnStubs.insert(Name);
453 }
454 }
455
456 if (GV->hasExternalWeakLinkage())
457 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 int Offset = MO.getOffset();
460 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000461 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 else if (Offset < 0)
463 O << Offset;
464
465 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000466 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 O << "@TLSGD"; // general dynamic TLS model
468 else
469 if (GV->isDeclaration())
470 O << "@INDNTPOFF"; // initial exec TLS model
471 else
472 O << "@NTPOFF"; // local exec TLS model
473 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000474 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
476 O << "@GOT";
477 else
478 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000479 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
480 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000481 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 O << "@GOTPCREL";
483
484 if (needCloseParen) {
485 needCloseParen = false;
486 O << ')';
487 }
488
489 // Use rip when possible to reduce code size, except when
490 // index or base register are also part of the address. e.g.
491 // foo(%rip)(%rcx,%rax,4) is not legal
492 O << "(%rip)";
493 }
494 }
495
496 if (needCloseParen)
497 O << ')';
498
499 return;
500 }
501 case MachineOperand::MO_ExternalSymbol: {
502 bool isCallOp = Modifier && !strcmp(Modifier, "call");
503 bool needCloseParen = false;
504 std::string Name(TAI->getGlobalPrefix());
505 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000506 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000508 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 return;
510 }
511 if (!isCallOp)
512 O << '$';
513 else if (Name[0] == '$') {
514 // The name begins with a dollar-sign. In order to avoid having it look
515 // like an integer immediate to the assembler, enclose it in parens.
516 O << '(';
517 needCloseParen = true;
518 }
519
520 O << Name;
521
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000522 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 std::string GOTName(TAI->getGlobalPrefix());
524 GOTName+="_GLOBAL_OFFSET_TABLE_";
525 if (Name == GOTName)
526 // HACK! Emit extra offset to PC during printing GOT offset to
527 // compensate for the size of popl instruction. The resulting code
528 // should look like:
529 // call .piclabel
530 // piclabel:
531 // popl %some_register
532 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
533 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000534 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535
536 if (isCallOp)
537 O << "@PLT";
538 }
539
540 if (needCloseParen)
541 O << ')';
542
543 if (!isCallOp && Subtarget->isPICStyleRIPRel())
544 O << "(%rip)";
545
546 return;
547 }
548 default:
549 O << "<unknown operand type>"; return;
550 }
551}
552
553void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000554 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 assert(value <= 7 && "Invalid ssecc argument!");
556 switch (value) {
557 case 0: O << "eq"; break;
558 case 1: O << "lt"; break;
559 case 2: O << "le"; break;
560 case 3: O << "unord"; break;
561 case 4: O << "neq"; break;
562 case 5: O << "nlt"; break;
563 case 6: O << "nle"; break;
564 case 7: O << "ord"; break;
565 }
566}
567
568void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
569 const char *Modifier){
570 assert(isMem(MI, Op) && "Invalid memory reference!");
571 MachineOperand BaseReg = MI->getOperand(Op);
572 MachineOperand IndexReg = MI->getOperand(Op+2);
573 const MachineOperand &DispSpec = MI->getOperand(Op+3);
574
575 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
576 if (DispSpec.isGlobalAddress() ||
577 DispSpec.isConstantPoolIndex() ||
578 DispSpec.isJumpTableIndex()) {
579 printOperand(MI, Op+3, "mem", NotRIPRel);
580 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000581 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
583 O << DispVal;
584 }
585
586 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000587 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000589
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 // There are cases where we can end up with ESP/RSP in the indexreg slot.
591 // If this happens, swap the base/index register to support assemblers that
592 // don't work when the index is *SP.
593 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
594 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
595 std::swap(BaseReg, IndexReg);
596 std::swap(BaseRegOperand, IndexRegOperand);
597 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000598
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000599 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 if (BaseReg.getReg())
601 printOperand(MI, Op+BaseRegOperand, Modifier);
602
603 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000604 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 printOperand(MI, Op+IndexRegOperand, Modifier);
606 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000607 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000609 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 }
611}
612
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000613void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000614 const MachineBasicBlock *MBB) const {
615 if (!TAI->getSetDirective())
616 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000617
618 // We don't need .set machinery if we have GOT-style relocations
619 if (Subtarget->isPICStyleGOT())
620 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000621
Evan Cheng6fb06762007-11-09 01:32:10 +0000622 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
623 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000624 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000625 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000626 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000627 << '_' << uid << '\n';
628 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000629 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000630}
631
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000633 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000634 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635}
636
637
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000638void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
639 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000640 unsigned uid) const
641{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000642 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
643 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
644
645 O << JTEntryDirective << ' ';
646
647 if (TM.getRelocationModel() == Reloc::PIC_) {
648 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
649 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
650 << '_' << uid << "_set_" << MBB->getNumber();
651 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000652 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000653 O << "@GOTOFF";
654 } else
655 assert(0 && "Don't know how to print MBB label for this PIC mode");
656 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000657 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000658}
659
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000660bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 unsigned Reg = MO.getReg();
663 switch (Mode) {
664 default: return true; // Unknown mode.
665 case 'b': // Print QImode register
666 Reg = getX86SubSuperRegister(Reg, MVT::i8);
667 break;
668 case 'h': // Print QImode high register
669 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
670 break;
671 case 'w': // Print HImode register
672 Reg = getX86SubSuperRegister(Reg, MVT::i16);
673 break;
674 case 'k': // Print SImode register
675 Reg = getX86SubSuperRegister(Reg, MVT::i32);
676 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000677 case 'q': // Print DImode register
678 Reg = getX86SubSuperRegister(Reg, MVT::i64);
679 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 }
681
Evan Cheng00d04a72008-07-07 22:21:06 +0000682 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 return false;
684}
685
686/// PrintAsmOperand - Print out an operand for an inline asm expression.
687///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000688bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000689 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 const char *ExtraCode) {
691 // Does this asm operand have a single letter operand modifier?
692 if (ExtraCode && ExtraCode[0]) {
693 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000694
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 switch (ExtraCode[0]) {
696 default: return true; // Unknown modifier.
697 case 'c': // Don't print "$" before a global var name or constant.
698 printOperand(MI, OpNo, "mem");
699 return false;
700 case 'b': // Print QImode register
701 case 'h': // Print QImode high register
702 case 'w': // Print HImode register
703 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000704 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000705 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
707 printOperand(MI, OpNo);
708 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000709
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 case 'P': // Don't print @PLT, but do print as memory.
711 printOperand(MI, OpNo, "mem");
712 return false;
713 }
714 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 printOperand(MI, OpNo);
717 return false;
718}
719
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000720bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000722 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000724 if (ExtraCode && ExtraCode[0]) {
725 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000726
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000727 switch (ExtraCode[0]) {
728 default: return true; // Unknown modifier.
729 case 'b': // Print QImode register
730 case 'h': // Print QImode high register
731 case 'w': // Print HImode register
732 case 'k': // Print SImode register
733 case 'q': // Print SImode register
734 // These only apply to registers, ignore on mem.
735 break;
736 }
737 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 printMemReference(MI, OpNo);
739 return false;
740}
741
742/// printMachineInstruction -- Print out a single X86 LLVM instruction
743/// MI in AT&T syntax to the current output stream.
744///
745void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
746 ++EmittedInsts;
747
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 // Call the autogenerated instruction printer routines.
749 printInstruction(MI);
750}
751
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000752/// doInitialization
753bool X86ATTAsmPrinter::doInitialization(Module &M) {
754 if (TAI->doesSupportDebugInformation()) {
755 // Emit initial debug information.
756 DW.BeginModule(&M);
757 }
758
759 bool Result = AsmPrinter::doInitialization(M);
760
761 // Darwin wants symbols to be quoted if they have complex names.
762 if (Subtarget->isTargetDarwin())
763 Mang->setUseQuotes(true);
764
765 return Result;
766}
767
768
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000769void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000770 const TargetData *TD = TM.getTargetData();
771
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000772 if (!GVar->hasInitializer())
773 return; // External global require no code
774
775 // Check to see if this is a special global used by LLVM, if so, emit it.
776 if (EmitSpecialLLVMGlobal(GVar)) {
777 if (Subtarget->isTargetDarwin() &&
778 TM.getRelocationModel() == Reloc::Static) {
779 if (GVar->getName() == "llvm.global_ctors")
780 O << ".reference .constructors_used\n";
781 else if (GVar->getName() == "llvm.global_dtors")
782 O << ".reference .destructors_used\n";
783 }
784 return;
785 }
786
787 std::string name = Mang->getValueName(GVar);
788 Constant *C = GVar->getInitializer();
789 const Type *Type = C->getType();
790 unsigned Size = TD->getABITypeSize(Type);
791 unsigned Align = TD->getPreferredAlignmentLog(GVar);
792
793 if (GVar->hasHiddenVisibility()) {
794 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000795 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000796 } else if (GVar->hasProtectedVisibility()) {
797 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000798 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000799 }
800
801 if (Subtarget->isTargetELF())
802 O << "\t.type\t" << name << ",@object\n";
803
804 if (C->isNullValue() && !GVar->hasSection()) {
805 if (GVar->hasExternalLinkage()) {
806 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 return;
811 }
812 }
813
814 if (!GVar->isThreadLocal() &&
815 (GVar->hasInternalLinkage() || GVar->hasWeakLinkage() ||
816 GVar->hasLinkOnceLinkage() || GVar->hasCommonLinkage())) {
817 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
818 if (!NoZerosInBSS && TAI->getBSSSection())
819 SwitchToDataSection(TAI->getBSSSection(), GVar);
820 else
821 SwitchToDataSection(TAI->getDataSection(), GVar);
822 if (TAI->getLCOMMDirective() != NULL) {
823 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000826 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000827 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000828 O << "\t.globl " << name << '\n'
829 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
831 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000832 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000833 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 EmitGlobalConstant(C);
836 return;
837 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839
840 // Leopard and above support aligned common symbols.
841 if (Subtarget->getDarwinVers() >= 9)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000842 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 }
844 } else {
845 if (!Subtarget->isTargetCygMing()) {
846 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000847 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000849 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000851 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000853 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000855 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000856 return;
857 }
858 }
859
860 switch (GVar->getLinkage()) {
861 case GlobalValue::CommonLinkage:
862 case GlobalValue::LinkOnceLinkage:
863 case GlobalValue::WeakLinkage:
864 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000865 O << "\t.globl " << name << '\n'
866 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867 if (!GVar->isConstant())
868 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
869 else {
870 const ArrayType *AT = dyn_cast<ArrayType>(Type);
871 if (AT && AT->getElementType()==Type::Int8Ty)
872 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", GVar);
873 else
874 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", GVar);
875 }
876 } else if (Subtarget->isTargetCygMing()) {
877 std::string SectionName(".section\t.data$linkonce." +
878 name +
879 ",\"aw\"");
880 SwitchToDataSection(SectionName.c_str(), GVar);
881 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000882 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883 } else {
884 std::string SectionName("\t.section\t.llvm.linkonce.d." +
885 name +
886 ",\"aw\",@progbits");
887 SwitchToDataSection(SectionName.c_str(), GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000888 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000889 }
890 break;
891 case GlobalValue::DLLExportLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000892 case GlobalValue::AppendingLinkage:
893 // FIXME: appending linkage variables should go into a section of
894 // their name or something. For now, just emit them as external.
895 case GlobalValue::ExternalLinkage:
896 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000897 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000898 // FALL THROUGH
899 case GlobalValue::InternalLinkage: {
900 if (GVar->isConstant()) {
901 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
902 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
903 SwitchToDataSection(TAI->getCStringSection(), GVar);
904 break;
905 }
906 }
907 // FIXME: special handling for ".ctors" & ".dtors" sections
908 if (GVar->hasSection() &&
909 (GVar->getSection() == ".ctors" || GVar->getSection() == ".dtors")) {
910 std::string SectionName = ".section " + GVar->getSection();
911
912 if (Subtarget->isTargetCygMing()) {
913 SectionName += ",\"aw\"";
914 } else {
915 assert(!Subtarget->isTargetDarwin());
916 SectionName += ",\"aw\",@progbits";
917 }
918 SwitchToDataSection(SectionName.c_str());
919 } else if (GVar->hasSection() && Subtarget->isTargetDarwin()) {
920 // Honor all section names on Darwin; ObjC uses this
921 std::string SectionName = ".section " + GVar->getSection();
922 SwitchToDataSection(SectionName.c_str());
923 } else {
924 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
925 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSBSSSection() :
926 TAI->getBSSSection(), GVar);
927 else if (!GVar->isConstant())
928 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSDataSection() :
929 TAI->getDataSection(), GVar);
930 else if (GVar->isThreadLocal())
931 SwitchToDataSection(TAI->getTLSDataSection());
932 else {
933 // Read-only data.
934 bool HasReloc = C->ContainsRelocations();
935 if (HasReloc &&
936 Subtarget->isTargetDarwin() &&
937 TM.getRelocationModel() != Reloc::Static)
938 SwitchToDataSection("\t.const_data\n");
939 else if (!HasReloc && Size == 4 &&
940 TAI->getFourByteConstantSection())
941 SwitchToDataSection(TAI->getFourByteConstantSection(), GVar);
942 else if (!HasReloc && Size == 8 &&
943 TAI->getEightByteConstantSection())
944 SwitchToDataSection(TAI->getEightByteConstantSection(), GVar);
945 else if (!HasReloc && Size == 16 &&
946 TAI->getSixteenByteConstantSection())
947 SwitchToDataSection(TAI->getSixteenByteConstantSection(), GVar);
948 else if (TAI->getReadOnlySection())
949 SwitchToDataSection(TAI->getReadOnlySection(), GVar);
950 else
951 SwitchToDataSection(TAI->getDataSection(), GVar);
952 }
953 }
954
955 break;
956 }
957 default:
958 assert(0 && "Unknown linkage type!");
959 }
960
961 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000962 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000963 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000964 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000965 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000966 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000967
968 // If the initializer is a extern weak symbol, remember to emit the weak
969 // reference!
970 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
971 if (GV->hasExternalWeakLinkage())
972 ExtWeakSymbols.insert(GV);
973
974 EmitGlobalConstant(C);
975}
976
977
978bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000979 // Print out module-level global variables here.
980 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000981 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000982 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000983
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000984 if (I->hasDLLExportLinkage())
985 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
986 }
987
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000988 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000989 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000990 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000991
992 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
993 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000994 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000995 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000996
997 if (!DLLExportedFns.empty()) {
998 SwitchToDataSection(".section .drectve");
999 }
1000
1001 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1002 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001003 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001004 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001005
1006 if (Subtarget->isTargetDarwin()) {
1007 SwitchToDataSection("");
1008
1009 // Output stubs for dynamically-linked functions
1010 unsigned j = 1;
1011 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1012 i != e; ++i, ++j) {
1013 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1014 "self_modifying_code+pure_instructions,5", 0);
1015 std::string p = i->getKeyData();
1016 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001017 O << ":\n"
1018 "\t.indirect_symbol " << p << "\n"
1019 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001020 }
1021
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001022 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001023
1024 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1025 // Add the (possibly multiple) personalities to the set of global values.
1026 // Only referenced functions get into the Personalities list.
1027 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1028
1029 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1030 E = Personalities.end(); I != E; ++I)
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001031 if (*I) GVStubs.insert('_' + (*I)->getName());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001032 }
1033
1034 // Output stubs for external and common global variables.
1035 if (!GVStubs.empty())
1036 SwitchToDataSection(
1037 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1038 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1039 i != e; ++i) {
1040 std::string p = i->getKeyData();
1041 printSuffixedName(p, "$non_lazy_ptr");
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001042 O << ":\n"
1043 "\t.indirect_symbol " << p << "\n"
1044 "\t.long\t0\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001045 }
1046
1047 // Emit final debug information.
1048 DW.EndModule();
1049
1050 // Funny Darwin hack: This flag tells the linker that no global symbols
1051 // contain code that falls through to other global symbols (e.g. the obvious
1052 // implementation of multiple entry points). If this doesn't occur, the
1053 // linker can safely perform dead code stripping. Since LLVM never
1054 // generates code that does this, it is always safe to set.
1055 O << "\t.subsections_via_symbols\n";
1056 } else if (Subtarget->isTargetCygMing()) {
1057 // Emit type information for external functions
1058 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1059 i != e; ++i) {
1060 O << "\t.def\t " << i->getKeyData()
1061 << ";\t.scl\t" << COFF::C_EXT
1062 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1063 << ";\t.endef\n";
1064 }
1065
1066 // Emit final debug information.
1067 DW.EndModule();
1068 } else if (Subtarget->isTargetELF()) {
1069 // Emit final debug information.
1070 DW.EndModule();
1071 }
1072
1073 return AsmPrinter::doFinalization(M);
1074}
1075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076// Include the auto-generated portion of the assembly writer.
1077#include "X86GenAsmWriter.inc"