blob: 7c03f9da36cf0a4844089795c9de2dcc66b95e0c [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetAsmInfo.h"
33#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
36STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
Evan Cheng0729ccf2008-01-05 00:41:47 +000038static std::string getPICLabelString(unsigned FnNum,
39 const TargetAsmInfo *TAI,
40 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 std::string label;
42 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000043 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000045 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 else
47 assert(0 && "Don't know how to print PIC label!\n");
48
49 return label;
50}
51
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000052static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
53 const TargetData *TD) {
54 X86MachineFunctionInfo Info;
55 uint64_t Size = 0;
56
57 switch (F->getCallingConv()) {
58 case CallingConv::X86_StdCall:
59 Info.setDecorationStyle(StdCall);
60 break;
61 case CallingConv::X86_FastCall:
62 Info.setDecorationStyle(FastCall);
63 break;
64 default:
65 return Info;
66 }
67
68 unsigned argNum = 1;
69 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
70 AI != AE; ++AI, ++argNum) {
71 const Type* Ty = AI->getType();
72
73 // 'Dereference' type in case of byval parameter attribute
74 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
75 Ty = cast<PointerType>(Ty)->getElementType();
76
77 // Size should be aligned to DWORD boundary
78 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
79 }
80
81 // We're not supporting tooooo huge arguments :)
82 Info.setBytesToPopOnReturn((unsigned int)Size);
83 return Info;
84}
85
86/// PrintUnmangledNameSafely - Print out the printable characters in the name.
87/// Don't print things like \n or \0.
Owen Anderson847b99b2008-08-21 00:14:44 +000088static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000089 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
90 Name != E; ++Name)
91 if (isprint(*Name))
92 OS << *Name;
93}
94
95/// decorateName - Query FunctionInfoMap and use this information for various
96/// name decoration.
97void X86ATTAsmPrinter::decorateName(std::string &Name,
98 const GlobalValue *GV) {
99 const Function *F = dyn_cast<Function>(GV);
100 if (!F) return;
101
102 // We don't want to decorate non-stdcall or non-fastcall functions right now
103 unsigned CC = F->getCallingConv();
104 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
105 return;
106
107 // Decorate names only when we're targeting Cygwin/Mingw32 targets
108 if (!Subtarget->isTargetCygMing())
109 return;
110
111 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
112
113 const X86MachineFunctionInfo *Info;
114 if (info_item == FunctionInfoMap.end()) {
115 // Calculate apropriate function info and populate map
116 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
117 Info = &FunctionInfoMap[F];
118 } else {
119 Info = &info_item->second;
120 }
121
122 const FunctionType *FT = F->getFunctionType();
123 switch (Info->getDecorationStyle()) {
124 case None:
125 break;
126 case StdCall:
127 // "Pure" variadic functions do not receive @0 suffix.
128 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
129 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
130 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
131 break;
132 case FastCall:
133 // "Pure" variadic functions do not receive @0 suffix.
134 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
135 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
137
138 if (Name[0] == '_') {
139 Name[0] = '@';
140 } else {
141 Name = '@' + Name;
142 }
143 break;
144 default:
145 assert(0 && "Unsupported DecorationStyle");
146 }
147}
148
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000149// Substitute old hook with new one temporary
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000151 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152}
153
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000154void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 const Function *F = MF.getFunction();
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000156 std::string SectionName = TAI->SectionForGlobal(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000158 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Anton Korobeynikov8b967b52008-07-09 13:28:19 +0000160 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000161
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000162 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 switch (F->getLinkage()) {
164 default: assert(0 && "Unknown linkage type!");
165 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000166 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 break;
168 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000170 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000171 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 break;
173 case Function::LinkOnceLinkage:
174 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
178 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 }
185 break;
186 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000187
188 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000191 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 else if (Subtarget->isTargetCygMing()) {
193 O << "\t.def\t " << CurrentFnName
194 << ";\t.scl\t" <<
195 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
196 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
197 << ";\t.endef\n";
198 }
199
200 O << CurrentFnName << ":\n";
201 // Add some workaround for linkonce linkage on Cygwin\MinGW
202 if (Subtarget->isTargetCygMing() &&
203 (F->getLinkage() == Function::LinkOnceLinkage ||
204 F->getLinkage() == Function::WeakLinkage))
205 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000206}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000208/// runOnMachineFunction - This uses the printMachineInstruction()
209/// method to print assembly for each instruction.
210///
211bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
212 const Function *F = MF.getFunction();
213 unsigned CC = F->getCallingConv();
214
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000215 SetupMachineFunction(MF);
216 O << "\n\n";
217
218 // Populate function information map. Actually, We don't want to populate
219 // non-stdcall or non-fastcall functions' information right now.
220 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
221 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
222
223 // Print out constants referenced by the function
224 EmitConstantPool(MF.getConstantPool());
225
226 if (F->hasDLLExportLinkage())
227 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
228
229 // Print the 'header' of function
230 emitFunctionHeader(MF);
231
232 // Emit pre-function debug and/or EH information.
233 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
234 DW.BeginFunction(&MF);
235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000237 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
239 I != E; ++I) {
240 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000241 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000242 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 O << '\n';
244 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000245 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
246 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000248 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000249 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 printMachineInstruction(II);
251 }
252 }
253
Dale Johannesenf35771f2008-04-08 00:37:56 +0000254 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
255 // If the function is empty, then we need to emit *something*. Otherwise,
256 // the function's label might be associated with something that it wasn't
257 // meant to be associated with. We emit a noop in this situation.
258 // We are assuming inline asms are code.
259 O << "\tnop\n";
260 }
261
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000263 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000265 // Emit post-function debug information.
266 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268
269 // Print out jump tables referenced by the function.
270 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 // We didn't modify anything.
273 return false;
274}
275
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000276static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
278}
279
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000280static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
281 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
282 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
283}
284
285static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
287}
288
289void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
290 const char *Modifier, bool NotRIPRel) {
291 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 switch (MO.getType()) {
293 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000294 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 "Virtual registers should not make it this far!");
296 O << '%';
297 unsigned Reg = MO.getReg();
298 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000299 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
301 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
302 Reg = getX86SubSuperRegister(Reg, VT);
303 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000304 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 return;
306 }
307
308 case MachineOperand::MO_Immediate:
309 if (!Modifier ||
310 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
311 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000312 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 return;
314 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000315 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 return;
317 case MachineOperand::MO_JumpTableIndex: {
318 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
319 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000320 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000321 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322
323 if (TM.getRelocationModel() == Reloc::PIC_) {
324 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000325 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
326 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 else if (Subtarget->isPICStyleGOT())
328 O << "@GOTOFF";
329 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000330
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
332 O << "(%rip)";
333 return;
334 }
335 case MachineOperand::MO_ConstantPoolIndex: {
336 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
337 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000338 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000339 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340
341 if (TM.getRelocationModel() == Reloc::PIC_) {
342 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000343 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
344 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 else if (Subtarget->isPICStyleGOT())
346 O << "@GOTOFF";
347 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000348
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 int Offset = MO.getOffset();
350 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000351 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 else if (Offset < 0)
353 O << Offset;
354
355 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
356 O << "(%rip)";
357 return;
358 }
359 case MachineOperand::MO_GlobalAddress: {
360 bool isCallOp = Modifier && !strcmp(Modifier, "call");
361 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
362 bool needCloseParen = false;
363
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000364 const GlobalValue *GV = MO.getGlobal();
365 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
366 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000367 // If GV is an alias then use the aliasee for determining
368 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000369 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
370 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
371 }
372
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 bool isThreadLocal = GVar && GVar->isThreadLocal();
374
375 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000376 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000377
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 if (!isMemOp && !isCallOp)
379 O << '$';
380 else if (Name[0] == '$') {
381 // The name begins with a dollar-sign. In order to avoid having it look
382 // like an integer immediate to the assembler, enclose it in parens.
383 O << '(';
384 needCloseParen = true;
385 }
386
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000387 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 // Link-once, declaration, or Weakly-linked global variables need
389 // non-lazily-resolved stubs
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000390 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 // Dynamically-resolved functions need a stub for the function.
392 if (isCallOp && isa<Function>(GV)) {
393 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000394 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 } else {
396 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000397 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 }
399 } else {
400 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000401 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 O << Name;
403 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000404
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000406 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 } else {
408 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000409 O << "__imp_";
410 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 O << Name;
412
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000413 if (isCallOp) {
414 if (shouldPrintPLT(TM, Subtarget)) {
415 // Assemble call via PLT for externally visible symbols
416 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
417 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 O << "@PLT";
419 }
420 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
421 // Save function name for later type emission
422 FnStubs.insert(Name);
423 }
424 }
425
426 if (GV->hasExternalWeakLinkage())
427 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000428
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 int Offset = MO.getOffset();
430 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000431 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 else if (Offset < 0)
433 O << Offset;
434
435 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000436 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 O << "@TLSGD"; // general dynamic TLS model
438 else
439 if (GV->isDeclaration())
440 O << "@INDNTPOFF"; // initial exec TLS model
441 else
442 O << "@NTPOFF"; // local exec TLS model
443 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000444 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
446 O << "@GOT";
447 else
448 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000449 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
450 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000451 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 O << "@GOTPCREL";
453
454 if (needCloseParen) {
455 needCloseParen = false;
456 O << ')';
457 }
458
459 // Use rip when possible to reduce code size, except when
460 // index or base register are also part of the address. e.g.
461 // foo(%rip)(%rcx,%rax,4) is not legal
462 O << "(%rip)";
463 }
464 }
465
466 if (needCloseParen)
467 O << ')';
468
469 return;
470 }
471 case MachineOperand::MO_ExternalSymbol: {
472 bool isCallOp = Modifier && !strcmp(Modifier, "call");
473 bool needCloseParen = false;
474 std::string Name(TAI->getGlobalPrefix());
475 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000476 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000478 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 return;
480 }
481 if (!isCallOp)
482 O << '$';
483 else if (Name[0] == '$') {
484 // The name begins with a dollar-sign. In order to avoid having it look
485 // like an integer immediate to the assembler, enclose it in parens.
486 O << '(';
487 needCloseParen = true;
488 }
489
490 O << Name;
491
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000492 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 std::string GOTName(TAI->getGlobalPrefix());
494 GOTName+="_GLOBAL_OFFSET_TABLE_";
495 if (Name == GOTName)
496 // HACK! Emit extra offset to PC during printing GOT offset to
497 // compensate for the size of popl instruction. The resulting code
498 // should look like:
499 // call .piclabel
500 // piclabel:
501 // popl %some_register
502 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
503 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000504 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505
506 if (isCallOp)
507 O << "@PLT";
508 }
509
510 if (needCloseParen)
511 O << ')';
512
513 if (!isCallOp && Subtarget->isPICStyleRIPRel())
514 O << "(%rip)";
515
516 return;
517 }
518 default:
519 O << "<unknown operand type>"; return;
520 }
521}
522
523void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000524 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 assert(value <= 7 && "Invalid ssecc argument!");
526 switch (value) {
527 case 0: O << "eq"; break;
528 case 1: O << "lt"; break;
529 case 2: O << "le"; break;
530 case 3: O << "unord"; break;
531 case 4: O << "neq"; break;
532 case 5: O << "nlt"; break;
533 case 6: O << "nle"; break;
534 case 7: O << "ord"; break;
535 }
536}
537
538void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
539 const char *Modifier){
540 assert(isMem(MI, Op) && "Invalid memory reference!");
541 MachineOperand BaseReg = MI->getOperand(Op);
542 MachineOperand IndexReg = MI->getOperand(Op+2);
543 const MachineOperand &DispSpec = MI->getOperand(Op+3);
544
545 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
546 if (DispSpec.isGlobalAddress() ||
547 DispSpec.isConstantPoolIndex() ||
548 DispSpec.isJumpTableIndex()) {
549 printOperand(MI, Op+3, "mem", NotRIPRel);
550 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000551 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
553 O << DispVal;
554 }
555
556 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000557 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000559
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 // There are cases where we can end up with ESP/RSP in the indexreg slot.
561 // If this happens, swap the base/index register to support assemblers that
562 // don't work when the index is *SP.
563 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
564 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
565 std::swap(BaseReg, IndexReg);
566 std::swap(BaseRegOperand, IndexRegOperand);
567 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000568
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000569 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 if (BaseReg.getReg())
571 printOperand(MI, Op+BaseRegOperand, Modifier);
572
573 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000574 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 printOperand(MI, Op+IndexRegOperand, Modifier);
576 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000577 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000579 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 }
581}
582
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000583void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000584 const MachineBasicBlock *MBB) const {
585 if (!TAI->getSetDirective())
586 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000587
588 // We don't need .set machinery if we have GOT-style relocations
589 if (Subtarget->isPICStyleGOT())
590 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000591
Evan Cheng6fb06762007-11-09 01:32:10 +0000592 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
593 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000594 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000595 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000596 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000597 << '_' << uid << '\n';
598 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000599 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000600}
601
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000603 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000604 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605}
606
607
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000608void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
609 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000610 unsigned uid) const
611{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000612 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
613 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
614
615 O << JTEntryDirective << ' ';
616
617 if (TM.getRelocationModel() == Reloc::PIC_) {
618 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
619 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
620 << '_' << uid << "_set_" << MBB->getNumber();
621 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000622 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000623 O << "@GOTOFF";
624 } else
625 assert(0 && "Don't know how to print MBB label for this PIC mode");
626 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000627 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000628}
629
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000630bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 unsigned Reg = MO.getReg();
633 switch (Mode) {
634 default: return true; // Unknown mode.
635 case 'b': // Print QImode register
636 Reg = getX86SubSuperRegister(Reg, MVT::i8);
637 break;
638 case 'h': // Print QImode high register
639 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
640 break;
641 case 'w': // Print HImode register
642 Reg = getX86SubSuperRegister(Reg, MVT::i16);
643 break;
644 case 'k': // Print SImode register
645 Reg = getX86SubSuperRegister(Reg, MVT::i32);
646 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000647 case 'q': // Print DImode register
648 Reg = getX86SubSuperRegister(Reg, MVT::i64);
649 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 }
651
Evan Cheng00d04a72008-07-07 22:21:06 +0000652 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 return false;
654}
655
656/// PrintAsmOperand - Print out an operand for an inline asm expression.
657///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000658bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000659 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 const char *ExtraCode) {
661 // Does this asm operand have a single letter operand modifier?
662 if (ExtraCode && ExtraCode[0]) {
663 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000664
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 switch (ExtraCode[0]) {
666 default: return true; // Unknown modifier.
667 case 'c': // Don't print "$" before a global var name or constant.
668 printOperand(MI, OpNo, "mem");
669 return false;
670 case 'b': // Print QImode register
671 case 'h': // Print QImode high register
672 case 'w': // Print HImode register
673 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000674 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000675 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
677 printOperand(MI, OpNo);
678 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000679
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 case 'P': // Don't print @PLT, but do print as memory.
681 printOperand(MI, OpNo, "mem");
682 return false;
683 }
684 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000685
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 printOperand(MI, OpNo);
687 return false;
688}
689
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000690bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000692 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000694 if (ExtraCode && ExtraCode[0]) {
695 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000696
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000697 switch (ExtraCode[0]) {
698 default: return true; // Unknown modifier.
699 case 'b': // Print QImode register
700 case 'h': // Print QImode high register
701 case 'w': // Print HImode register
702 case 'k': // Print SImode register
703 case 'q': // Print SImode register
704 // These only apply to registers, ignore on mem.
705 break;
706 }
707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 printMemReference(MI, OpNo);
709 return false;
710}
711
712/// printMachineInstruction -- Print out a single X86 LLVM instruction
713/// MI in AT&T syntax to the current output stream.
714///
715void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
716 ++EmittedInsts;
717
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 // Call the autogenerated instruction printer routines.
719 printInstruction(MI);
720}
721
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000722/// doInitialization
723bool X86ATTAsmPrinter::doInitialization(Module &M) {
724 if (TAI->doesSupportDebugInformation()) {
725 // Emit initial debug information.
726 DW.BeginModule(&M);
727 }
728
Evan Cheng5cda7762008-07-09 06:36:53 +0000729 bool Result = AsmPrinter::doInitialization(M);
730
Dale Johannesen06545922008-07-09 20:55:35 +0000731 if (TAI->doesSupportDebugInformation()) {
732 // Let PassManager know we need debug information and relay
733 // the MachineModuleInfo address on to DwarfWriter.
734 // AsmPrinter::doInitialization did this analysis.
735 MMI = getAnalysisToUpdate<MachineModuleInfo>();
736 DW.SetModuleInfo(MMI);
737 }
738
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000739 // Darwin wants symbols to be quoted if they have complex names.
740 if (Subtarget->isTargetDarwin())
741 Mang->setUseQuotes(true);
742
743 return Result;
744}
745
746
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000747void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000748 const TargetData *TD = TM.getTargetData();
749
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000750 if (!GVar->hasInitializer())
751 return; // External global require no code
752
753 // Check to see if this is a special global used by LLVM, if so, emit it.
754 if (EmitSpecialLLVMGlobal(GVar)) {
755 if (Subtarget->isTargetDarwin() &&
756 TM.getRelocationModel() == Reloc::Static) {
757 if (GVar->getName() == "llvm.global_ctors")
758 O << ".reference .constructors_used\n";
759 else if (GVar->getName() == "llvm.global_dtors")
760 O << ".reference .destructors_used\n";
761 }
762 return;
763 }
764
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000765 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000766 std::string name = Mang->getValueName(GVar);
767 Constant *C = GVar->getInitializer();
768 const Type *Type = C->getType();
769 unsigned Size = TD->getABITypeSize(Type);
770 unsigned Align = TD->getPreferredAlignmentLog(GVar);
771
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000772 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000773
774 if (Subtarget->isTargetELF())
775 O << "\t.type\t" << name << ",@object\n";
776
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000777 SwitchToDataSection(SectionName.c_str());
778
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000779 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000780 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000781 if (GVar->hasExternalLinkage()) {
782 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000783 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000784 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000785 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000786 return;
787 }
788 }
789
790 if (!GVar->isThreadLocal() &&
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000791 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000792 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000793
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000794 if (TAI->getLCOMMDirective() != NULL) {
795 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000796 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000797 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000798 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000799 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000800 O << "\t.globl " << name << '\n'
801 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000802 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000803 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000804 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000805 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 EmitGlobalConstant(C);
807 return;
808 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000810 if (TAI->getCOMMDirectiveTakesAlignment())
811 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000812 }
813 } else {
814 if (!Subtarget->isTargetCygMing()) {
815 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000816 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000817 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000818 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000819 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000820 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 return;
826 }
827 }
828
829 switch (GVar->getLinkage()) {
Evan Cheng630c5612008-08-08 06:43:59 +0000830 case GlobalValue::CommonLinkage:
831 case GlobalValue::LinkOnceLinkage:
832 case GlobalValue::WeakLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000833 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << "\t.globl " << name << '\n'
835 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000836 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000837 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000838 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000839 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000840 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 }
842 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000843 case GlobalValue::DLLExportLinkage:
844 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 // FIXME: appending linkage variables should go into a section of
846 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000847 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000848 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000849 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 // FALL THROUGH
Evan Cheng630c5612008-08-08 06:43:59 +0000851 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000853 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 assert(0 && "Unknown linkage type!");
855 }
856
857 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000859 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000860 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000861 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863
864 // If the initializer is a extern weak symbol, remember to emit the weak
865 // reference!
866 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
867 if (GV->hasExternalWeakLinkage())
868 ExtWeakSymbols.insert(GV);
869
870 EmitGlobalConstant(C);
871}
872
Evan Cheng76443dc2008-07-08 00:55:58 +0000873/// printGVStub - Print stub for a global value.
874///
875void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000876 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000877 O << ":\n\t.indirect_symbol ";
878 if (Prefix) O << Prefix;
879 O << GV << "\n\t.long\t0\n";
880}
881
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000882
883bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000884 // Print out module-level global variables here.
885 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000886 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000887 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000888
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000889 if (I->hasDLLExportLinkage())
890 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
891 }
892
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000893 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000894 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000895 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000896
897 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
898 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000899 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000900 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000901
902 if (!DLLExportedFns.empty()) {
903 SwitchToDataSection(".section .drectve");
904 }
905
906 for (StringSet<>::iterator i = DLLExportedFns.begin(),
907 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000908 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000910
911 if (Subtarget->isTargetDarwin()) {
912 SwitchToDataSection("");
913
914 // Output stubs for dynamically-linked functions
915 unsigned j = 1;
916 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
917 i != e; ++i, ++j) {
918 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
919 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000920 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000921 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000922 O << ":\n"
923 "\t.indirect_symbol " << p << "\n"
924 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000925 }
926
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000927 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000928
Evan Cheng76443dc2008-07-08 00:55:58 +0000929 // Print global value stubs.
930 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000931 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
932 // Add the (possibly multiple) personalities to the set of global values.
933 // Only referenced functions get into the Personalities list.
934 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000935 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000936 E = Personalities.end(); I != E; ++I) {
937 if (!*I)
938 continue;
939 if (!InStubSection) {
940 SwitchToDataSection(
941 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
942 InStubSection = true;
943 }
944 printGVStub((*I)->getNameStart(), "_");
945 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000946 }
947
948 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000949 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000950 SwitchToDataSection(
951 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
952 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000953 i != e; ++i)
954 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000955
956 // Emit final debug information.
957 DW.EndModule();
958
959 // Funny Darwin hack: This flag tells the linker that no global symbols
960 // contain code that falls through to other global symbols (e.g. the obvious
961 // implementation of multiple entry points). If this doesn't occur, the
962 // linker can safely perform dead code stripping. Since LLVM never
963 // generates code that does this, it is always safe to set.
964 O << "\t.subsections_via_symbols\n";
965 } else if (Subtarget->isTargetCygMing()) {
966 // Emit type information for external functions
967 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
968 i != e; ++i) {
969 O << "\t.def\t " << i->getKeyData()
970 << ";\t.scl\t" << COFF::C_EXT
971 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
972 << ";\t.endef\n";
973 }
974
975 // Emit final debug information.
976 DW.EndModule();
977 } else if (Subtarget->isTargetELF()) {
978 // Emit final debug information.
979 DW.EndModule();
980 }
981
982 return AsmPrinter::doFinalization(M);
983}
984
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985// Include the auto-generated portion of the assembly writer.
986#include "X86GenAsmWriter.inc"