blob: 60ed4f0c946a1a8edd93db628f4357a2cb4be429 [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"
Chris Lattner19b1bd52009-06-19 00:47:33 +000029#include "llvm/MC/MCInst.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000030#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000032#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000034#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037using namespace llvm;
38
39STATISTIC(EmittedInsts, "Number of machine instrs printed");
40
Chris Lattner19b1bd52009-06-19 00:47:33 +000041static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
42 cl::Hidden);
43
Evan Cheng0729ccf2008-01-05 00:41:47 +000044static std::string getPICLabelString(unsigned FnNum,
45 const TargetAsmInfo *TAI,
46 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 std::string label;
48 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000049 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000051 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 else
53 assert(0 && "Don't know how to print PIC label!\n");
54
55 return label;
56}
57
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000058static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
59 const TargetData *TD) {
60 X86MachineFunctionInfo Info;
61 uint64_t Size = 0;
62
63 switch (F->getCallingConv()) {
64 case CallingConv::X86_StdCall:
65 Info.setDecorationStyle(StdCall);
66 break;
67 case CallingConv::X86_FastCall:
68 Info.setDecorationStyle(FastCall);
69 break;
70 default:
71 return Info;
72 }
73
74 unsigned argNum = 1;
75 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
76 AI != AE; ++AI, ++argNum) {
77 const Type* Ty = AI->getType();
78
79 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000080 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000081 Ty = cast<PointerType>(Ty)->getElementType();
82
83 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000084 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000085 }
86
87 // We're not supporting tooooo huge arguments :)
88 Info.setBytesToPopOnReturn((unsigned int)Size);
89 return Info;
90}
91
92/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +000093/// Don't print things like \\n or \\0.
Owen Anderson847b99b2008-08-21 00:14:44 +000094static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000095 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
96 Name != E; ++Name)
97 if (isprint(*Name))
98 OS << *Name;
99}
100
101/// decorateName - Query FunctionInfoMap and use this information for various
102/// name decoration.
103void X86ATTAsmPrinter::decorateName(std::string &Name,
104 const GlobalValue *GV) {
105 const Function *F = dyn_cast<Function>(GV);
106 if (!F) return;
107
108 // We don't want to decorate non-stdcall or non-fastcall functions right now
109 unsigned CC = F->getCallingConv();
110 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
111 return;
112
113 // Decorate names only when we're targeting Cygwin/Mingw32 targets
114 if (!Subtarget->isTargetCygMing())
115 return;
116
117 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
118
119 const X86MachineFunctionInfo *Info;
120 if (info_item == FunctionInfoMap.end()) {
121 // Calculate apropriate function info and populate map
122 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
123 Info = &FunctionInfoMap[F];
124 } else {
125 Info = &info_item->second;
126 }
127
128 const FunctionType *FT = F->getFunctionType();
129 switch (Info->getDecorationStyle()) {
130 case None:
131 break;
132 case StdCall:
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 break;
138 case FastCall:
139 // "Pure" variadic functions do not receive @0 suffix.
140 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
141 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
142 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
143
144 if (Name[0] == '_') {
145 Name[0] = '@';
146 } else {
147 Name = '@' + Name;
148 }
149 break;
150 default:
151 assert(0 && "Unsupported DecorationStyle");
152 }
153}
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();
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 Korobeynikov1a9edae2008-09-24 22:14:23 +0000160 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000161
Devang Patel93698d92008-10-01 23:18:38 +0000162 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000163 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000164 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 switch (F->getLinkage()) {
166 default: assert(0 && "Unknown linkage type!");
167 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000168 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000169 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
171 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000173 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000174 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000176 case Function::LinkOnceAnyLinkage:
177 case Function::LinkOnceODRLinkage:
178 case Function::WeakAnyLinkage:
179 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000180 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000182 O << "\t.globl\t" << CurrentFnName << '\n';
183 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000185 O << "\t.globl\t" << CurrentFnName << "\n"
186 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 break;
191 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000192
193 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
195 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000196 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 else if (Subtarget->isTargetCygMing()) {
198 O << "\t.def\t " << CurrentFnName
199 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000200 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
202 << ";\t.endef\n";
203 }
204
205 O << CurrentFnName << ":\n";
206 // Add some workaround for linkonce linkage on Cygwin\MinGW
207 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000208 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000210}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212/// runOnMachineFunction - This uses the printMachineInstruction()
213/// method to print assembly for each instruction.
214///
215bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
216 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000217 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000218 unsigned CC = F->getCallingConv();
219
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000220 SetupMachineFunction(MF);
221 O << "\n\n";
222
223 // Populate function information map. Actually, We don't want to populate
224 // non-stdcall or non-fastcall functions' information right now.
225 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
226 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
227
228 // Print out constants referenced by the function
229 EmitConstantPool(MF.getConstantPool());
230
231 if (F->hasDLLExportLinkage())
232 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
233
234 // Print the 'header' of function
235 emitFunctionHeader(MF);
236
237 // Emit pre-function debug and/or EH information.
238 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000239 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000242 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
244 I != E; ++I) {
245 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000246 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
247 // This is an entry block or a block that's only reachable via a
248 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
249 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000250 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 O << '\n';
252 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000253 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
254 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000256 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000257 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 printMachineInstruction(II);
259 }
260 }
261
Dale Johannesenf35771f2008-04-08 00:37:56 +0000262 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
263 // If the function is empty, then we need to emit *something*. Otherwise,
264 // the function's label might be associated with something that it wasn't
265 // meant to be associated with. We emit a noop in this situation.
266 // We are assuming inline asms are code.
267 O << "\tnop\n";
268 }
269
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000271 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000273 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000274 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000275 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276
277 // Print out jump tables referenced by the function.
278 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000279
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000280 O.flush();
281
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 // We didn't modify anything.
283 return false;
284}
285
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000286static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
288}
289
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000290static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
291 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
292 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
293}
294
295static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
297}
298
Chris Lattner357a0ca2009-06-20 19:34:09 +0000299/// print_pcrel_imm - This is used to print an immediate value that ends up
300/// being encoded as a pc-relative value. These print slightly differently, for
301/// example, a $ is not emitted.
302void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
303 const MachineOperand &MO = MI->getOperand(OpNo);
304 switch (MO.getType()) {
305 default: assert(0 && "Unknown pcrel immediate operand");
306 case MachineOperand::MO_Immediate:
307 O << MO.getImm();
308 return;
309 case MachineOperand::MO_MachineBasicBlock:
310 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
311 return;
312
313 case MachineOperand::MO_GlobalAddress: {
314 const GlobalValue *GV = MO.getGlobal();
315 std::string Name = Mang->getValueName(GV);
316 decorateName(Name, GV);
317
318 bool needCloseParen = false;
319 if (Name[0] == '$') {
320 // The name begins with a dollar-sign. In order to avoid having it look
321 // like an integer immediate to the assembler, enclose it in parens.
322 O << '(';
323 needCloseParen = true;
324 }
325
326 if (shouldPrintStub(TM, Subtarget)) {
327 // Link-once, declaration, or Weakly-linked global variables need
328 // non-lazily-resolved stubs
329 if (GV->isDeclaration() || GV->isWeakForLinker()) {
330 // Dynamically-resolved functions need a stub for the function.
331 if (isa<Function>(GV)) {
332 // Function stubs are no longer needed for Mac OS X 10.5 and up.
333 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
334 O << Name;
335 } else {
336 FnStubs.insert(Name);
337 printSuffixedName(Name, "$stub");
338 }
339 } else if (GV->hasHiddenVisibility()) {
340 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
341 // Definition is not definitely in the current translation unit.
342 O << Name;
343 else {
344 HiddenGVStubs.insert(Name);
345 printSuffixedName(Name, "$non_lazy_ptr");
346 }
347 } else {
348 GVStubs.insert(Name);
349 printSuffixedName(Name, "$non_lazy_ptr");
350 }
351 } else {
352 if (GV->hasDLLImportLinkage())
353 O << "__imp_";
354 O << Name;
355 }
356 } else {
357 if (GV->hasDLLImportLinkage()) {
358 O << "__imp_";
359 }
360 O << Name;
361
362 if (shouldPrintPLT(TM, Subtarget)) {
363 // Assemble call via PLT for externally visible symbols
364 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
365 !GV->hasLocalLinkage())
366 O << "@PLT";
367 }
368 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
369 // Save function name for later type emission
370 FnStubs.insert(Name);
371 }
372
373 if (GV->hasExternalWeakLinkage())
374 ExtWeakSymbols.insert(GV);
375
376 printOffset(MO.getOffset());
377
378 if (needCloseParen)
379 O << ')';
380 return;
381 }
382
383 case MachineOperand::MO_ExternalSymbol: {
384 bool needCloseParen = false;
385 std::string Name(TAI->getGlobalPrefix());
386 Name += MO.getSymbolName();
387 // Print function stub suffix unless it's Mac OS X 10.5 and up.
388 if (shouldPrintStub(TM, Subtarget) &&
389 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
390 FnStubs.insert(Name);
391 printSuffixedName(Name, "$stub");
392 return;
393 }
394
395 if (Name[0] == '$') {
396 // The name begins with a dollar-sign. In order to avoid having it look
397 // like an integer immediate to the assembler, enclose it in parens.
398 O << '(';
399 needCloseParen = true;
400 }
401
402 O << Name;
403
404 if (shouldPrintPLT(TM, Subtarget)) {
405 std::string GOTName(TAI->getGlobalPrefix());
406 GOTName+="_GLOBAL_OFFSET_TABLE_";
407 if (Name == GOTName)
408 // HACK! Emit extra offset to PC during printing GOT offset to
409 // compensate for the size of popl instruction. The resulting code
410 // should look like:
411 // call .piclabel
412 // piclabel:
413 // popl %some_register
414 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
415 O << " + [.-"
416 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
417
418 O << "@PLT";
419 }
420
421 if (needCloseParen)
422 O << ')';
423
424 return;
425 }
426 }
427}
428
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
430 const char *Modifier, bool NotRIPRel) {
431 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 switch (MO.getType()) {
433 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000434 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 "Virtual registers should not make it this far!");
436 O << '%';
437 unsigned Reg = MO.getReg();
438 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000439 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
441 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
442 Reg = getX86SubSuperRegister(Reg, VT);
443 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000444 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 return;
446 }
447
448 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000449 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000450 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000452 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 case MachineOperand::MO_JumpTableIndex: {
455 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
456 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000457 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000458 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459
460 if (TM.getRelocationModel() == Reloc::PIC_) {
461 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000462 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
463 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 else if (Subtarget->isPICStyleGOT())
465 O << "@GOTOFF";
466 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000467
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
469 O << "(%rip)";
470 return;
471 }
472 case MachineOperand::MO_ConstantPoolIndex: {
473 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
474 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000475 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000476 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477
478 if (TM.getRelocationModel() == Reloc::PIC_) {
479 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000480 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
481 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 else if (Subtarget->isPICStyleGOT())
483 O << "@GOTOFF";
484 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000485
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000486 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487
488 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
489 O << "(%rip)";
490 return;
491 }
492 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000493 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 bool needCloseParen = false;
495
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000496 const GlobalValue *GV = MO.getGlobal();
497 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
498 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000499 // If GV is an alias then use the aliasee for determining
500 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000501 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000502 GVar =dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000503 }
504
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 bool isThreadLocal = GVar && GVar->isThreadLocal();
506
507 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000508 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000509
Chris Lattner357a0ca2009-06-20 19:34:09 +0000510 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 O << '$';
512 else if (Name[0] == '$') {
513 // The name begins with a dollar-sign. In order to avoid having it look
514 // like an integer immediate to the assembler, enclose it in parens.
515 O << '(';
516 needCloseParen = true;
517 }
518
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000519 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 // Link-once, declaration, or Weakly-linked global variables need
521 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000522 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000524 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-12-05 01:06:39 +0000525 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
526 // Definition is not definitely in the current translation unit.
527 O << Name;
528 else {
529 HiddenGVStubs.insert(Name);
530 printSuffixedName(Name, "$non_lazy_ptr");
531 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 } else {
533 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000534 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 }
536 } else {
537 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000538 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 O << Name;
540 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000541
Chris Lattner357a0ca2009-06-20 19:34:09 +0000542 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000543 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000545 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000546 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 }
549
550 if (GV->hasExternalWeakLinkage())
551 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000552
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000553 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554
Chris Lattnerdabfe572009-06-21 02:22:53 +0000555 if (needCloseParen)
556 O << ')';
557
558 bool isRIPRelative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000560 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
561 switch (model) {
562 case TLSModel::GeneralDynamic:
563 O << "@TLSGD";
564 break;
565 case TLSModel::LocalDynamic:
566 // O << "@TLSLD"; // local dynamic not implemented
Bill Wendlingc1946742009-05-30 01:09:53 +0000567 O << "@TLSGD";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000568 break;
569 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000570 if (Subtarget->is64Bit()) {
571 assert (!NotRIPRel);
Chris Lattnerdabfe572009-06-21 02:22:53 +0000572 O << "@GOTTPOFF";
573 isRIPRelative = true;
Rafael Espindolab93a5122009-04-13 13:02:49 +0000574 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000575 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000576 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000577 break;
578 case TLSModel::LocalExec:
579 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000580 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000581 else
Bill Wendlingc1946742009-05-30 01:09:53 +0000582 O << "@NTPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000583 break;
584 default:
585 assert (0 && "Unknown TLS model");
586 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000588 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
590 O << "@GOT";
591 else
592 O << "@GOTOFF";
Chris Lattnerdabfe572009-06-21 02:22:53 +0000593 } else if (Subtarget->isPICStyleRIPRel() &&
594 !NotRIPRel) {
Dan Gohmane254f322009-03-14 02:33:41 +0000595 if (TM.getRelocationModel() != Reloc::Static) {
596 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
597 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 }
Chris Lattnerdabfe572009-06-21 02:22:53 +0000599
600 isRIPRelative = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 }
602 }
603
Chris Lattnerdabfe572009-06-21 02:22:53 +0000604 // Use rip when possible to reduce code size, except when
605 // index or base register are also part of the address. e.g.
606 // foo(%rip)(%rcx,%rax,4) is not legal.
607 if (isRIPRelative)
608 O << "(%rip)";
609
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 return;
611 }
612 case MachineOperand::MO_ExternalSymbol: {
Dan Gohman96f096e2009-04-23 00:57:37 +0000613 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 bool needCloseParen = false;
615 std::string Name(TAI->getGlobalPrefix());
616 Name += MO.getSymbolName();
Chris Lattner357a0ca2009-06-20 19:34:09 +0000617
Evan Chengdfd884e2008-09-20 00:13:45 +0000618 // Print function stub suffix unless it's Mac OS X 10.5 and up.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000619 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 O << '$';
621 else if (Name[0] == '$') {
622 // The name begins with a dollar-sign. In order to avoid having it look
623 // like an integer immediate to the assembler, enclose it in parens.
624 O << '(';
625 needCloseParen = true;
626 }
627
628 O << Name;
629
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000630 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 std::string GOTName(TAI->getGlobalPrefix());
632 GOTName+="_GLOBAL_OFFSET_TABLE_";
633 if (Name == GOTName)
634 // HACK! Emit extra offset to PC during printing GOT offset to
635 // compensate for the size of popl instruction. The resulting code
636 // should look like:
637 // call .piclabel
638 // piclabel:
639 // popl %some_register
640 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
641 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000642 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 }
644
645 if (needCloseParen)
646 O << ')';
647
Chris Lattner357a0ca2009-06-20 19:34:09 +0000648 if (Subtarget->isPICStyleRIPRel())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 O << "(%rip)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 return;
651 }
652 default:
653 O << "<unknown operand type>"; return;
654 }
655}
656
657void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000658 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 assert(value <= 7 && "Invalid ssecc argument!");
660 switch (value) {
661 case 0: O << "eq"; break;
662 case 1: O << "lt"; break;
663 case 2: O << "le"; break;
664 case 3: O << "unord"; break;
665 case 4: O << "neq"; break;
666 case 5: O << "nlt"; break;
667 case 6: O << "nle"; break;
668 case 7: O << "ord"; break;
669 }
670}
671
Rafael Espindolabca99f72009-04-08 21:14:34 +0000672void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000673 const char *Modifier,
674 bool NotRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 MachineOperand BaseReg = MI->getOperand(Op);
676 MachineOperand IndexReg = MI->getOperand(Op+2);
677 const MachineOperand &DispSpec = MI->getOperand(Op+3);
678
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000679 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000680 if (DispSpec.isGlobal() ||
681 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000682 DispSpec.isJTI() ||
683 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 printOperand(MI, Op+3, "mem", NotRIPRel);
685 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000686 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
688 O << DispVal;
689 }
690
691 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000692 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000694
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 // There are cases where we can end up with ESP/RSP in the indexreg slot.
696 // If this happens, swap the base/index register to support assemblers that
697 // don't work when the index is *SP.
698 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
699 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
700 std::swap(BaseReg, IndexReg);
701 std::swap(BaseRegOperand, IndexRegOperand);
702 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000703
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000704 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 if (BaseReg.getReg())
706 printOperand(MI, Op+BaseRegOperand, Modifier);
707
708 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000709 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 printOperand(MI, Op+IndexRegOperand, Modifier);
711 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000712 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000714 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 }
716}
717
Rafael Espindolabca99f72009-04-08 21:14:34 +0000718void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000719 const char *Modifier, bool NotRIPRel){
Rafael Espindolabca99f72009-04-08 21:14:34 +0000720 assert(isMem(MI, Op) && "Invalid memory reference!");
721 MachineOperand Segment = MI->getOperand(Op+4);
722 if (Segment.getReg()) {
723 printOperand(MI, Op+4, Modifier);
724 O << ':';
725 }
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000726 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000727}
728
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000729void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000730 const MachineBasicBlock *MBB) const {
731 if (!TAI->getSetDirective())
732 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000733
734 // We don't need .set machinery if we have GOT-style relocations
735 if (Subtarget->isPICStyleGOT())
736 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000737
Evan Cheng6fb06762007-11-09 01:32:10 +0000738 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
739 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000740 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000741 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000742 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000743 << '_' << uid << '\n';
744 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000745 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000746}
747
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000749 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000750 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751}
752
753
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000754void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
755 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000756 unsigned uid) const
757{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000758 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
759 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
760
761 O << JTEntryDirective << ' ';
762
763 if (TM.getRelocationModel() == Reloc::PIC_) {
764 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
765 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
766 << '_' << uid << "_set_" << MBB->getNumber();
767 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000768 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000769 O << "@GOTOFF";
770 } else
771 assert(0 && "Don't know how to print MBB label for this PIC mode");
772 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000773 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000774}
775
Chris Lattner8bb96e42009-06-15 04:42:32 +0000776bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777 unsigned Reg = MO.getReg();
778 switch (Mode) {
779 default: return true; // Unknown mode.
780 case 'b': // Print QImode register
781 Reg = getX86SubSuperRegister(Reg, MVT::i8);
782 break;
783 case 'h': // Print QImode high register
784 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
785 break;
786 case 'w': // Print HImode register
787 Reg = getX86SubSuperRegister(Reg, MVT::i16);
788 break;
789 case 'k': // Print SImode register
790 Reg = getX86SubSuperRegister(Reg, MVT::i32);
791 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000792 case 'q': // Print DImode register
793 Reg = getX86SubSuperRegister(Reg, MVT::i64);
794 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 }
796
Evan Cheng00d04a72008-07-07 22:21:06 +0000797 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 return false;
799}
800
801/// PrintAsmOperand - Print out an operand for an inline asm expression.
802///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000803bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000804 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 const char *ExtraCode) {
806 // Does this asm operand have a single letter operand modifier?
807 if (ExtraCode && ExtraCode[0]) {
808 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000809
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 switch (ExtraCode[0]) {
811 default: return true; // Unknown modifier.
812 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000813 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 return false;
815 case 'b': // Print QImode register
816 case 'h': // Print QImode high register
817 case 'w': // Print HImode register
818 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000819 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000820 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
822 printOperand(MI, OpNo);
823 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000824
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000826 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 return false;
828 }
829 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000830
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 printOperand(MI, OpNo);
832 return false;
833}
834
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000835bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000837 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000839 if (ExtraCode && ExtraCode[0]) {
840 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000841
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000842 switch (ExtraCode[0]) {
843 default: return true; // Unknown modifier.
844 case 'b': // Print QImode register
845 case 'h': // Print QImode high register
846 case 'w': // Print HImode register
847 case 'k': // Print SImode register
848 case 'q': // Print SImode register
849 // These only apply to registers, ignore on mem.
850 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000851 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000852 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattnerd1595722009-01-23 22:33:40 +0000853 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000854 }
855 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 printMemReference(MI, OpNo);
857 return false;
858}
859
Chris Lattnerf5da5902009-06-20 07:03:18 +0000860static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
861 // Convert registers in the addr mode according to subreg64.
862 for (unsigned i = 0; i != 4; ++i) {
863 if (!MI->getOperand(i).isReg()) continue;
864
865 unsigned Reg = MI->getOperand(i).getReg();
866 if (Reg == 0) continue;
867
868 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
869 }
870}
871
Bill Wendlingb3b11262009-02-06 21:45:08 +0000872/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
873/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874///
875void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
876 ++EmittedInsts;
877
Chris Lattner19b1bd52009-06-19 00:47:33 +0000878 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000879 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
880 O << "\t";
881 printInlineAsm(MI);
882 return;
883 } else if (MI->isLabel()) {
884 printLabel(MI);
885 return;
886 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
887 printDeclare(MI);
888 return;
889 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
890 printImplicitDef(MI);
891 return;
892 }
893
Chris Lattnere67184e2009-06-19 23:59:57 +0000894 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000895 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000896
897 TmpInst.setOpcode(MI->getOpcode());
898
899 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
900 const MachineOperand &MO = MI->getOperand(i);
901
902 MCOperand MCOp;
903 if (MO.isReg()) {
904 MCOp.MakeReg(MO.getReg());
905 } else if (MO.isImm()) {
906 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000907 } else if (MO.isMBB()) {
908 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000909 } else {
910 assert(0 && "Unimp");
911 }
912
913 TmpInst.addOperand(MCOp);
914 }
915
Chris Lattner6b9f7742009-06-20 07:59:10 +0000916 switch (TmpInst.getOpcode()) {
917 case X86::LEA64_32r:
918 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000919 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000920 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000921 }
922
Chris Lattner19b1bd52009-06-19 00:47:33 +0000923 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000924 printInstruction(&TmpInst);
925 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000926 }
927
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 // Call the autogenerated instruction printer routines.
929 printInstruction(MI);
930}
931
Devang Patelc20079e2009-06-20 01:00:07 +0000932/// doInitialization
933bool X86ATTAsmPrinter::doInitialization(Module &M) {
934 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
935 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
936 return AsmPrinter::doInitialization(M);
937}
938
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000939void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000940 const TargetData *TD = TM.getTargetData();
941
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000942 if (!GVar->hasInitializer())
943 return; // External global require no code
944
945 // Check to see if this is a special global used by LLVM, if so, emit it.
946 if (EmitSpecialLLVMGlobal(GVar)) {
947 if (Subtarget->isTargetDarwin() &&
948 TM.getRelocationModel() == Reloc::Static) {
949 if (GVar->getName() == "llvm.global_ctors")
950 O << ".reference .constructors_used\n";
951 else if (GVar->getName() == "llvm.global_dtors")
952 O << ".reference .destructors_used\n";
953 }
954 return;
955 }
956
957 std::string name = Mang->getValueName(GVar);
958 Constant *C = GVar->getInitializer();
959 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000960 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000961 unsigned Align = TD->getPreferredAlignmentLog(GVar);
962
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000963 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000964
965 if (Subtarget->isTargetELF())
966 O << "\t.type\t" << name << ",@object\n";
967
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000968 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000969
Evan Chengcf84b142009-02-18 02:19:52 +0000970 if (C->isNullValue() && !GVar->hasSection() &&
971 !(Subtarget->isTargetDarwin() &&
972 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000973 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000974 if (GVar->hasExternalLinkage()) {
975 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000976 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000977 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000978 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000979 return;
980 }
981 }
982
983 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000984 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000985 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000986
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000987 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000988 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000989 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000990 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000991 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000992 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000993 O << "\t.globl " << name << '\n'
994 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000995 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000996 O << name << ":";
997 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000998 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000999 PrintUnmangledNameSafely(GVar, O);
1000 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001001 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001002 EmitGlobalConstant(C);
1003 return;
1004 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001005 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +00001006 if (TAI->getCOMMDirectiveTakesAlignment())
1007 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001008 }
1009 } else {
1010 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001011 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001012 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001013 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001014 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001015 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001016 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001017 }
Evan Cheng11db8142009-03-24 00:17:40 +00001018 if (VerboseAsm) {
1019 O << "\t\t" << TAI->getCommentString() << ' ';
1020 PrintUnmangledNameSafely(GVar, O);
1021 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001022 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001023 return;
1024 }
1025 }
1026
1027 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001028 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001029 case GlobalValue::LinkOnceAnyLinkage:
1030 case GlobalValue::LinkOnceODRLinkage:
1031 case GlobalValue::WeakAnyLinkage:
1032 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001033 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001034 O << "\t.globl " << name << '\n'
1035 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001036 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001037 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001038 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001039 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001040 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001041 }
1042 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001043 case GlobalValue::DLLExportLinkage:
1044 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001045 // FIXME: appending linkage variables should go into a section of
1046 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +00001047 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001048 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001049 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001050 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001051 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001052 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001053 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001054 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001055 assert(0 && "Unknown linkage type!");
1056 }
1057
1058 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001059 O << name << ":";
1060 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +00001061 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001062 PrintUnmangledNameSafely(GVar, O);
1063 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001064 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001065 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001066 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001067
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001068 EmitGlobalConstant(C);
1069}
1070
Evan Cheng76443dc2008-07-08 00:55:58 +00001071/// printGVStub - Print stub for a global value.
1072///
1073void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +00001074 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +00001075 O << ":\n\t.indirect_symbol ";
1076 if (Prefix) O << Prefix;
1077 O << GV << "\n\t.long\t0\n";
1078}
1079
Evan Chenga65854f2008-12-05 01:06:39 +00001080/// printHiddenGVStub - Print stub for a hidden global value.
1081///
1082void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
1083 EmitAlignment(2);
1084 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
1085 if (Prefix) O << Prefix;
1086 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
1087}
1088
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001089
1090bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001091 // Print out module-level global variables here.
1092 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001093 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001094 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001095
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001096 if (I->hasDLLExportLinkage())
1097 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001098
1099 // If the global is a extern weak symbol, remember to emit the weak
1100 // reference!
Mike Stump5dae8792009-05-14 23:22:47 +00001101 // FIXME: This is rather hacky, since we'll emit references to ALL weak
1102 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001103 // initializers hidden deep inside constant expressions.
1104 if (I->hasExternalWeakLinkage())
1105 ExtWeakSymbols.insert(I);
1106 }
1107
1108 for (Module::const_iterator I = M.begin(), E = M.end();
1109 I != E; ++I) {
1110 // If the global is a extern weak symbol, remember to emit the weak
1111 // reference!
Mike Stump809b1092009-05-14 23:23:37 +00001112 // FIXME: This is rather hacky, since we'll emit references to ALL weak
1113 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001114 // initializers hidden deep inside constant expressions.
1115 if (I->hasExternalWeakLinkage())
1116 ExtWeakSymbols.insert(I);
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001117 }
1118
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001119 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001120 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001121 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001122
1123 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1124 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001125 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001126 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001127
1128 if (!DLLExportedFns.empty()) {
1129 SwitchToDataSection(".section .drectve");
1130 }
1131
1132 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1133 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001134 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001135 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001136
1137 if (Subtarget->isTargetDarwin()) {
1138 SwitchToDataSection("");
1139
1140 // Output stubs for dynamically-linked functions
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001141 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chenga65854f2008-12-05 01:06:39 +00001142 i != e; ++i) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001143 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1144 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +00001145 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001146 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001147 O << ":\n"
1148 "\t.indirect_symbol " << p << "\n"
1149 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001150 }
1151
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001152 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001153
Evan Cheng76443dc2008-07-08 00:55:58 +00001154 // Print global value stubs.
1155 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001156 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1157 // Add the (possibly multiple) personalities to the set of global values.
1158 // Only referenced functions get into the Personalities list.
1159 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001160 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +00001161 E = Personalities.end(); I != E; ++I) {
1162 if (!*I)
1163 continue;
1164 if (!InStubSection) {
1165 SwitchToDataSection(
1166 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1167 InStubSection = true;
1168 }
1169 printGVStub((*I)->getNameStart(), "_");
1170 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001171 }
1172
1173 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +00001174 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001175 SwitchToDataSection(
1176 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1177 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +00001178 i != e; ++i)
1179 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001180
Evan Chenga65854f2008-12-05 01:06:39 +00001181 if (!HiddenGVStubs.empty()) {
1182 SwitchToSection(TAI->getDataSection());
1183 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1184 i != e; ++i)
1185 printHiddenGVStub(i->getKeyData());
1186 }
1187
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001188 // Emit final debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +00001189 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel1a454842009-06-19 21:54:26 +00001190 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001191
1192 // Funny Darwin hack: This flag tells the linker that no global symbols
1193 // contain code that falls through to other global symbols (e.g. the obvious
1194 // implementation of multiple entry points). If this doesn't occur, the
1195 // linker can safely perform dead code stripping. Since LLVM never
1196 // generates code that does this, it is always safe to set.
1197 O << "\t.subsections_via_symbols\n";
1198 } else if (Subtarget->isTargetCygMing()) {
1199 // Emit type information for external functions
1200 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1201 i != e; ++i) {
1202 O << "\t.def\t " << i->getKeyData()
1203 << ";\t.scl\t" << COFF::C_EXT
1204 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1205 << ";\t.endef\n";
1206 }
1207
1208 // Emit final debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +00001209 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel1a454842009-06-19 21:54:26 +00001210 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001211 } else if (Subtarget->isTargetELF()) {
1212 // Emit final debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +00001213 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel1a454842009-06-19 21:54:26 +00001214 DW->EndModule();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001215 }
1216
1217 return AsmPrinter::doFinalization(M);
1218}
1219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220// Include the auto-generated portion of the assembly writer.
1221#include "X86GenAsmWriter.inc"