blob: 61556baea1b13d7d9e3413574bf8dd695e8f94de [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
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000150// Substitute old hook with new one temporary
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000152 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153}
154
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000155void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 const Function *F = MF.getFunction();
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000157 std::string SectionName = TAI->SectionForGlobal(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000159 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000161 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000162
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000163 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 switch (F->getLinkage()) {
165 default: assert(0 && "Unknown linkage type!");
166 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000167 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 break;
169 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000171 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000172 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 break;
174 case Function::LinkOnceLinkage:
175 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000176 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000178 O << "\t.globl\t" << CurrentFnName << '\n';
179 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << CurrentFnName << "\n"
182 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000184 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 }
186 break;
187 }
188 if (F->hasHiddenVisibility()) {
189 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000190 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 } else if (F->hasProtectedVisibility()) {
192 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000193 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 }
195
196 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000197 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 else if (Subtarget->isTargetCygMing()) {
199 O << "\t.def\t " << CurrentFnName
200 << ";\t.scl\t" <<
201 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
202 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
203 << ";\t.endef\n";
204 }
205
206 O << CurrentFnName << ":\n";
207 // Add some workaround for linkonce linkage on Cygwin\MinGW
208 if (Subtarget->isTargetCygMing() &&
209 (F->getLinkage() == Function::LinkOnceLinkage ||
210 F->getLinkage() == Function::WeakLinkage))
211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
219 unsigned CC = F->getCallingConv();
220
Evan Cheng5cda7762008-07-09 06:36:53 +0000221 if (TAI->doesSupportDebugInformation()) {
222 // Let PassManager know we need debug information and relay
223 // the MachineModuleInfo address on to DwarfWriter.
224 MMI = &getAnalysis<MachineModuleInfo>();
225 DW.SetModuleInfo(MMI);
226 }
227
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000228 SetupMachineFunction(MF);
229 O << "\n\n";
230
231 // Populate function information map. Actually, We don't want to populate
232 // non-stdcall or non-fastcall functions' information right now.
233 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
234 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
235
236 // Print out constants referenced by the function
237 EmitConstantPool(MF.getConstantPool());
238
239 if (F->hasDLLExportLinkage())
240 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
241
242 // Print the 'header' of function
243 emitFunctionHeader(MF);
244
245 // Emit pre-function debug and/or EH information.
246 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
247 DW.BeginFunction(&MF);
248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000250 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
252 I != E; ++I) {
253 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000254 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000255 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 O << '\n';
257 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000258 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
259 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000261 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000262 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 printMachineInstruction(II);
264 }
265 }
266
Dale Johannesenf35771f2008-04-08 00:37:56 +0000267 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
268 // If the function is empty, then we need to emit *something*. Otherwise,
269 // the function's label might be associated with something that it wasn't
270 // meant to be associated with. We emit a noop in this situation.
271 // We are assuming inline asms are code.
272 O << "\tnop\n";
273 }
274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000276 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000278 // Emit post-function debug information.
279 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281
282 // Print out jump tables referenced by the function.
283 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000284
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 // We didn't modify anything.
286 return false;
287}
288
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000289static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
291}
292
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000293static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
294 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
295 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
296}
297
298static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
300}
301
302void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
303 const char *Modifier, bool NotRIPRel) {
304 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 switch (MO.getType()) {
306 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000307 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 "Virtual registers should not make it this far!");
309 O << '%';
310 unsigned Reg = MO.getReg();
311 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000312 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
314 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
315 Reg = getX86SubSuperRegister(Reg, VT);
316 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000317 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 return;
319 }
320
321 case MachineOperand::MO_Immediate:
322 if (!Modifier ||
323 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
324 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000325 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 return;
327 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000328 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 return;
330 case MachineOperand::MO_JumpTableIndex: {
331 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
332 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000333 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000334 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335
336 if (TM.getRelocationModel() == Reloc::PIC_) {
337 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000338 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
339 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 else if (Subtarget->isPICStyleGOT())
341 O << "@GOTOFF";
342 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000343
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
345 O << "(%rip)";
346 return;
347 }
348 case MachineOperand::MO_ConstantPoolIndex: {
349 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
350 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000351 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000352 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353
354 if (TM.getRelocationModel() == Reloc::PIC_) {
355 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000356 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
357 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 else if (Subtarget->isPICStyleGOT())
359 O << "@GOTOFF";
360 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000361
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 int Offset = MO.getOffset();
363 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000364 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 else if (Offset < 0)
366 O << Offset;
367
368 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
369 O << "(%rip)";
370 return;
371 }
372 case MachineOperand::MO_GlobalAddress: {
373 bool isCallOp = Modifier && !strcmp(Modifier, "call");
374 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
375 bool needCloseParen = false;
376
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000377 const GlobalValue *GV = MO.getGlobal();
378 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
379 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000380 // If GV is an alias then use the aliasee for determining
381 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000382 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
383 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
384 }
385
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 bool isThreadLocal = GVar && GVar->isThreadLocal();
387
388 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000389 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 if (!isMemOp && !isCallOp)
392 O << '$';
393 else if (Name[0] == '$') {
394 // The name begins with a dollar-sign. In order to avoid having it look
395 // like an integer immediate to the assembler, enclose it in parens.
396 O << '(';
397 needCloseParen = true;
398 }
399
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000400 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 // Link-once, declaration, or Weakly-linked global variables need
402 // non-lazily-resolved stubs
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000403 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 // Dynamically-resolved functions need a stub for the function.
405 if (isCallOp && isa<Function>(GV)) {
406 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000407 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 } else {
409 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000410 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 }
412 } else {
413 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000414 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 O << Name;
416 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000417
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000419 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 } else {
421 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000422 O << "__imp_";
423 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 O << Name;
425
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000426 if (isCallOp) {
427 if (shouldPrintPLT(TM, Subtarget)) {
428 // Assemble call via PLT for externally visible symbols
429 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
430 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 O << "@PLT";
432 }
433 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
434 // Save function name for later type emission
435 FnStubs.insert(Name);
436 }
437 }
438
439 if (GV->hasExternalWeakLinkage())
440 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000441
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 int Offset = MO.getOffset();
443 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000444 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 else if (Offset < 0)
446 O << Offset;
447
448 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000449 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 O << "@TLSGD"; // general dynamic TLS model
451 else
452 if (GV->isDeclaration())
453 O << "@INDNTPOFF"; // initial exec TLS model
454 else
455 O << "@NTPOFF"; // local exec TLS model
456 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000457 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
459 O << "@GOT";
460 else
461 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000462 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
463 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000464 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 O << "@GOTPCREL";
466
467 if (needCloseParen) {
468 needCloseParen = false;
469 O << ')';
470 }
471
472 // Use rip when possible to reduce code size, except when
473 // index or base register are also part of the address. e.g.
474 // foo(%rip)(%rcx,%rax,4) is not legal
475 O << "(%rip)";
476 }
477 }
478
479 if (needCloseParen)
480 O << ')';
481
482 return;
483 }
484 case MachineOperand::MO_ExternalSymbol: {
485 bool isCallOp = Modifier && !strcmp(Modifier, "call");
486 bool needCloseParen = false;
487 std::string Name(TAI->getGlobalPrefix());
488 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000489 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000491 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 return;
493 }
494 if (!isCallOp)
495 O << '$';
496 else if (Name[0] == '$') {
497 // The name begins with a dollar-sign. In order to avoid having it look
498 // like an integer immediate to the assembler, enclose it in parens.
499 O << '(';
500 needCloseParen = true;
501 }
502
503 O << Name;
504
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000505 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 std::string GOTName(TAI->getGlobalPrefix());
507 GOTName+="_GLOBAL_OFFSET_TABLE_";
508 if (Name == GOTName)
509 // HACK! Emit extra offset to PC during printing GOT offset to
510 // compensate for the size of popl instruction. The resulting code
511 // should look like:
512 // call .piclabel
513 // piclabel:
514 // popl %some_register
515 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
516 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000517 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518
519 if (isCallOp)
520 O << "@PLT";
521 }
522
523 if (needCloseParen)
524 O << ')';
525
526 if (!isCallOp && Subtarget->isPICStyleRIPRel())
527 O << "(%rip)";
528
529 return;
530 }
531 default:
532 O << "<unknown operand type>"; return;
533 }
534}
535
536void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000537 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 assert(value <= 7 && "Invalid ssecc argument!");
539 switch (value) {
540 case 0: O << "eq"; break;
541 case 1: O << "lt"; break;
542 case 2: O << "le"; break;
543 case 3: O << "unord"; break;
544 case 4: O << "neq"; break;
545 case 5: O << "nlt"; break;
546 case 6: O << "nle"; break;
547 case 7: O << "ord"; break;
548 }
549}
550
551void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
552 const char *Modifier){
553 assert(isMem(MI, Op) && "Invalid memory reference!");
554 MachineOperand BaseReg = MI->getOperand(Op);
555 MachineOperand IndexReg = MI->getOperand(Op+2);
556 const MachineOperand &DispSpec = MI->getOperand(Op+3);
557
558 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
559 if (DispSpec.isGlobalAddress() ||
560 DispSpec.isConstantPoolIndex() ||
561 DispSpec.isJumpTableIndex()) {
562 printOperand(MI, Op+3, "mem", NotRIPRel);
563 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000564 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
566 O << DispVal;
567 }
568
569 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000570 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000572
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 // There are cases where we can end up with ESP/RSP in the indexreg slot.
574 // If this happens, swap the base/index register to support assemblers that
575 // don't work when the index is *SP.
576 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
577 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
578 std::swap(BaseReg, IndexReg);
579 std::swap(BaseRegOperand, IndexRegOperand);
580 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000581
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000582 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 if (BaseReg.getReg())
584 printOperand(MI, Op+BaseRegOperand, Modifier);
585
586 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000587 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 printOperand(MI, Op+IndexRegOperand, Modifier);
589 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000590 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000592 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 }
594}
595
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000596void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000597 const MachineBasicBlock *MBB) const {
598 if (!TAI->getSetDirective())
599 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000600
601 // We don't need .set machinery if we have GOT-style relocations
602 if (Subtarget->isPICStyleGOT())
603 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000604
Evan Cheng6fb06762007-11-09 01:32:10 +0000605 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
606 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000607 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000608 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000609 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000610 << '_' << uid << '\n';
611 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000612 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000613}
614
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000616 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000617 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618}
619
620
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000621void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
622 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000623 unsigned uid) const
624{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000625 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
626 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
627
628 O << JTEntryDirective << ' ';
629
630 if (TM.getRelocationModel() == Reloc::PIC_) {
631 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
632 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
633 << '_' << uid << "_set_" << MBB->getNumber();
634 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000635 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000636 O << "@GOTOFF";
637 } else
638 assert(0 && "Don't know how to print MBB label for this PIC mode");
639 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000640 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000641}
642
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000643bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 unsigned Reg = MO.getReg();
646 switch (Mode) {
647 default: return true; // Unknown mode.
648 case 'b': // Print QImode register
649 Reg = getX86SubSuperRegister(Reg, MVT::i8);
650 break;
651 case 'h': // Print QImode high register
652 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
653 break;
654 case 'w': // Print HImode register
655 Reg = getX86SubSuperRegister(Reg, MVT::i16);
656 break;
657 case 'k': // Print SImode register
658 Reg = getX86SubSuperRegister(Reg, MVT::i32);
659 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000660 case 'q': // Print DImode register
661 Reg = getX86SubSuperRegister(Reg, MVT::i64);
662 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 }
664
Evan Cheng00d04a72008-07-07 22:21:06 +0000665 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 return false;
667}
668
669/// PrintAsmOperand - Print out an operand for an inline asm expression.
670///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000671bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000672 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 const char *ExtraCode) {
674 // Does this asm operand have a single letter operand modifier?
675 if (ExtraCode && ExtraCode[0]) {
676 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 switch (ExtraCode[0]) {
679 default: return true; // Unknown modifier.
680 case 'c': // Don't print "$" before a global var name or constant.
681 printOperand(MI, OpNo, "mem");
682 return false;
683 case 'b': // Print QImode register
684 case 'h': // Print QImode high register
685 case 'w': // Print HImode register
686 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000687 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000688 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
690 printOperand(MI, OpNo);
691 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000692
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 case 'P': // Don't print @PLT, but do print as memory.
694 printOperand(MI, OpNo, "mem");
695 return false;
696 }
697 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000698
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 printOperand(MI, OpNo);
700 return false;
701}
702
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000703bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000705 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000707 if (ExtraCode && ExtraCode[0]) {
708 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000709
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000710 switch (ExtraCode[0]) {
711 default: return true; // Unknown modifier.
712 case 'b': // Print QImode register
713 case 'h': // Print QImode high register
714 case 'w': // Print HImode register
715 case 'k': // Print SImode register
716 case 'q': // Print SImode register
717 // These only apply to registers, ignore on mem.
718 break;
719 }
720 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 printMemReference(MI, OpNo);
722 return false;
723}
724
725/// printMachineInstruction -- Print out a single X86 LLVM instruction
726/// MI in AT&T syntax to the current output stream.
727///
728void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
729 ++EmittedInsts;
730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 // Call the autogenerated instruction printer routines.
732 printInstruction(MI);
733}
734
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000735/// doInitialization
736bool X86ATTAsmPrinter::doInitialization(Module &M) {
737 if (TAI->doesSupportDebugInformation()) {
738 // Emit initial debug information.
739 DW.BeginModule(&M);
740 }
741
Evan Cheng5cda7762008-07-09 06:36:53 +0000742 bool Result = AsmPrinter::doInitialization(M);
743
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000744 // Darwin wants symbols to be quoted if they have complex names.
745 if (Subtarget->isTargetDarwin())
746 Mang->setUseQuotes(true);
747
748 return Result;
749}
750
751
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000752void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000753 const TargetData *TD = TM.getTargetData();
754
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000755 if (!GVar->hasInitializer())
756 return; // External global require no code
757
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000758 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikovb81503c2008-07-09 13:24:38 +0000759
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000760 // Check to see if this is a special global used by LLVM, if so, emit it.
761 if (EmitSpecialLLVMGlobal(GVar)) {
762 if (Subtarget->isTargetDarwin() &&
763 TM.getRelocationModel() == Reloc::Static) {
764 if (GVar->getName() == "llvm.global_ctors")
765 O << ".reference .constructors_used\n";
766 else if (GVar->getName() == "llvm.global_dtors")
767 O << ".reference .destructors_used\n";
768 }
769 return;
770 }
771
772 std::string name = Mang->getValueName(GVar);
773 Constant *C = GVar->getInitializer();
774 const Type *Type = C->getType();
775 unsigned Size = TD->getABITypeSize(Type);
776 unsigned Align = TD->getPreferredAlignmentLog(GVar);
777
778 if (GVar->hasHiddenVisibility()) {
779 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000780 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000781 } else if (GVar->hasProtectedVisibility()) {
782 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000783 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000784 }
785
786 if (Subtarget->isTargetELF())
787 O << "\t.type\t" << name << ",@object\n";
788
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000789 SwitchToDataSection(SectionName.c_str());
790
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000791 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000792 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000793 if (GVar->hasExternalLinkage()) {
794 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000795 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000796 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000797 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000798 return;
799 }
800 }
801
802 if (!GVar->isThreadLocal() &&
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000803 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000804 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000805
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 if (TAI->getLCOMMDirective() != NULL) {
807 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000808 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000809 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000810 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000811 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 O << "\t.globl " << name << '\n'
813 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000814 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000815 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000816 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000817 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000818 EmitGlobalConstant(C);
819 return;
820 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000821 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000822
823 // Leopard and above support aligned common symbols.
824 if (Subtarget->getDarwinVers() >= 9)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000825 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 }
827 } else {
828 if (!Subtarget->isTargetCygMing()) {
829 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000830 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000831 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000832 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000833 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000836 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000837 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839 return;
840 }
841 }
842
843 switch (GVar->getLinkage()) {
844 case GlobalValue::CommonLinkage:
845 case GlobalValue::LinkOnceLinkage:
846 case GlobalValue::WeakLinkage:
847 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000848 O << "\t.globl " << name << '\n'
849 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000851 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000852 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000853 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000854 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 }
856 break;
857 case GlobalValue::DLLExportLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000858 case GlobalValue::AppendingLinkage:
859 // FIXME: appending linkage variables should go into a section of
860 // their name or something. For now, just emit them as external.
861 case GlobalValue::ExternalLinkage:
862 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000863 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000864 // FALL THROUGH
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000865 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 break;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867 default:
868 assert(0 && "Unknown linkage type!");
869 }
870
871 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000872 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000873 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000874 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000875 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000876 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000877
878 // If the initializer is a extern weak symbol, remember to emit the weak
879 // reference!
880 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
881 if (GV->hasExternalWeakLinkage())
882 ExtWeakSymbols.insert(GV);
883
884 EmitGlobalConstant(C);
885}
886
Evan Cheng76443dc2008-07-08 00:55:58 +0000887/// printGVStub - Print stub for a global value.
888///
889void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000890 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000891 O << ":\n\t.indirect_symbol ";
892 if (Prefix) O << Prefix;
893 O << GV << "\n\t.long\t0\n";
894}
895
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000896
897bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000898 // Print out module-level global variables here.
899 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000900 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000901 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000902
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000903 if (I->hasDLLExportLinkage())
904 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
905 }
906
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000907 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000908 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000910
911 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
912 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000913 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000914 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000915
916 if (!DLLExportedFns.empty()) {
917 SwitchToDataSection(".section .drectve");
918 }
919
920 for (StringSet<>::iterator i = DLLExportedFns.begin(),
921 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000922 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000923 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000924
925 if (Subtarget->isTargetDarwin()) {
926 SwitchToDataSection("");
927
928 // Output stubs for dynamically-linked functions
929 unsigned j = 1;
930 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
931 i != e; ++i, ++j) {
932 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
933 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000934 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000935 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000936 O << ":\n"
937 "\t.indirect_symbol " << p << "\n"
938 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000939 }
940
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000941 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000942
Evan Cheng76443dc2008-07-08 00:55:58 +0000943 // Print global value stubs.
944 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000945 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
946 // Add the (possibly multiple) personalities to the set of global values.
947 // Only referenced functions get into the Personalities list.
948 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000949 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000950 E = Personalities.end(); I != E; ++I) {
951 if (!*I)
952 continue;
953 if (!InStubSection) {
954 SwitchToDataSection(
955 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
956 InStubSection = true;
957 }
958 printGVStub((*I)->getNameStart(), "_");
959 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000960 }
961
962 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000963 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000964 SwitchToDataSection(
965 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
966 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000967 i != e; ++i)
968 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000969
970 // Emit final debug information.
971 DW.EndModule();
972
973 // Funny Darwin hack: This flag tells the linker that no global symbols
974 // contain code that falls through to other global symbols (e.g. the obvious
975 // implementation of multiple entry points). If this doesn't occur, the
976 // linker can safely perform dead code stripping. Since LLVM never
977 // generates code that does this, it is always safe to set.
978 O << "\t.subsections_via_symbols\n";
979 } else if (Subtarget->isTargetCygMing()) {
980 // Emit type information for external functions
981 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
982 i != e; ++i) {
983 O << "\t.def\t " << i->getKeyData()
984 << ";\t.scl\t" << COFF::C_EXT
985 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
986 << ";\t.endef\n";
987 }
988
989 // Emit final debug information.
990 DW.EndModule();
991 } else if (Subtarget->isTargetELF()) {
992 // Emit final debug information.
993 DW.EndModule();
994 }
995
996 return AsmPrinter::doFinalization(M);
997}
998
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999// Include the auto-generated portion of the assembly writer.
1000#include "X86GenAsmWriter.inc"