blob: f4196fd50706cc1478f187281c4b9f145a65d042 [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
Cedric Venetd85f51a2008-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"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
Chris Lattner40e3c7a2009-06-24 05:46:28 +000029#include "llvm/MC/MCContext.h"
Chris Lattner475370b2009-06-19 00:47:33 +000030#include "llvm/MC/MCInst.h"
Chris Lattner40e3c7a2009-06-24 05:46:28 +000031#include "llvm/MC/MCStreamer.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000032#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner475370b2009-06-19 00:47:33 +000034#include "llvm/Support/CommandLine.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000036#include "llvm/Support/raw_ostream.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000037#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000039using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000040
Chris Lattner95b2c7d2006-12-19 22:59:26 +000041STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
Chris Lattner475370b2009-06-19 00:47:33 +000043static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
44 cl::Hidden);
45
Evan Cheng0475ab52008-01-05 00:41:47 +000046static std::string getPICLabelString(unsigned FnNum,
47 const TargetAsmInfo *TAI,
48 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000049 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000050 if (Subtarget->isTargetDarwin())
Evan Cheng347d39f2007-10-14 05:57:21 +000051 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000052 else if (Subtarget->isTargetELF())
Dan Gohmand19a53b2008-06-30 22:03:41 +000053 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000054 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000055 assert(0 && "Don't know how to print PIC label!\n");
56
57 return label;
58}
59
Anton Korobeynikov75b68822008-06-28 11:08:27 +000060static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
61 const TargetData *TD) {
62 X86MachineFunctionInfo Info;
63 uint64_t Size = 0;
64
65 switch (F->getCallingConv()) {
66 case CallingConv::X86_StdCall:
67 Info.setDecorationStyle(StdCall);
68 break;
69 case CallingConv::X86_FastCall:
70 Info.setDecorationStyle(FastCall);
71 break;
72 default:
73 return Info;
74 }
75
76 unsigned argNum = 1;
77 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
78 AI != AE; ++AI, ++argNum) {
79 const Type* Ty = AI->getType();
80
81 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000082 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000083 Ty = cast<PointerType>(Ty)->getElementType();
84
85 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000086 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000087 }
88
89 // We're not supporting tooooo huge arguments :)
90 Info.setBytesToPopOnReturn((unsigned int)Size);
91 return Info;
92}
93
94/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman0f8b53f2009-03-03 02:55:14 +000095/// Don't print things like \\n or \\0.
Owen Andersoncb371882008-08-21 00:14:44 +000096static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +000097 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
98 Name != E; ++Name)
99 if (isprint(*Name))
100 OS << *Name;
101}
102
103/// decorateName - Query FunctionInfoMap and use this information for various
104/// name decoration.
105void X86ATTAsmPrinter::decorateName(std::string &Name,
106 const GlobalValue *GV) {
107 const Function *F = dyn_cast<Function>(GV);
108 if (!F) return;
109
110 // We don't want to decorate non-stdcall or non-fastcall functions right now
111 unsigned CC = F->getCallingConv();
112 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
113 return;
114
115 // Decorate names only when we're targeting Cygwin/Mingw32 targets
116 if (!Subtarget->isTargetCygMing())
117 return;
118
119 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
120
121 const X86MachineFunctionInfo *Info;
122 if (info_item == FunctionInfoMap.end()) {
123 // Calculate apropriate function info and populate map
124 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
125 Info = &FunctionInfoMap[F];
126 } else {
127 Info = &info_item->second;
128 }
129
130 const FunctionType *FT = F->getFunctionType();
131 switch (Info->getDecorationStyle()) {
132 case None:
133 break;
134 case StdCall:
135 // "Pure" variadic functions do not receive @0 suffix.
136 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
137 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
138 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
139 break;
140 case FastCall:
141 // "Pure" variadic functions do not receive @0 suffix.
142 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
143 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
144 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
145
146 if (Name[0] == '_') {
147 Name[0] = '@';
148 } else {
149 Name = '@' + Name;
150 }
151 break;
152 default:
153 assert(0 && "Unsupported DecorationStyle");
154 }
155}
156
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000157void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000158 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000159
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000160 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000161
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000162 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov91364382008-06-28 11:08:09 +0000163
Devang Patel4ae641f2008-10-01 23:18:38 +0000164 unsigned FnAlign = 4;
Devang Patele4d4b8c2008-10-06 17:30:07 +0000165 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Pateldb100332008-09-04 21:03:41 +0000166 FnAlign = 1;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000167 switch (F->getLinkage()) {
168 default: assert(0 && "Unknown linkage type!");
169 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000170 case Function::PrivateLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000171 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000172 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000173 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000174 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000177 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000178 case Function::LinkOnceAnyLinkage:
179 case Function::LinkOnceODRLinkage:
180 case Function::WeakAnyLinkage:
181 case Function::WeakODRLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000182 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000183 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000184 O << "\t.globl\t" << CurrentFnName << '\n';
185 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000186 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << CurrentFnName << "\n"
188 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000189 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000190 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000191 }
192 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000193 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000194
195 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000196
197 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000198 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000199 else if (Subtarget->isTargetCygMing()) {
200 O << "\t.def\t " << CurrentFnName
201 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000202 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000203 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
204 << ";\t.endef\n";
205 }
206
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000207 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000208 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000209 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000210 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000212}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000219 this->MF = &MF;
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000220 unsigned CC = F->getCallingConv();
221
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000222 SetupMachineFunction(MF);
223 O << "\n\n";
224
225 // Populate function information map. Actually, We don't want to populate
226 // non-stdcall or non-fastcall functions' information right now.
227 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
228 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
229
230 // Print out constants referenced by the function
231 EmitConstantPool(MF.getConstantPool());
232
233 if (F->hasDLLExportLinkage())
234 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
235
236 // Print the 'header' of function
237 emitFunctionHeader(MF);
238
239 // Emit pre-function debug and/or EH information.
240 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000241 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000242
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000244 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246 I != E; ++I) {
247 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000248 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
249 // This is an entry block or a block that's only reachable via a
250 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
251 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000252 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000253 O << '\n';
254 }
Bill Wendling824a7212008-01-26 09:03:52 +0000255 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
256 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000257 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000258 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000259 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260 printMachineInstruction(II);
261 }
262 }
Evan Cheng67afece2006-08-28 22:14:16 +0000263
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000264 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
265 // If the function is empty, then we need to emit *something*. Otherwise,
266 // the function's label might be associated with something that it wasn't
267 // meant to be associated with. We emit a noop in this situation.
268 // We are assuming inline asms are code.
269 O << "\tnop\n";
270 }
271
Jim Laskey563321a2006-09-06 18:34:40 +0000272 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000273 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000275 // Emit post-function debug information.
Devang Patel5090f192009-06-19 23:21:20 +0000276 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000277 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000278
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000279 // Print out jump tables referenced by the function.
280 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000281
Dan Gohmane5f4de42008-11-07 19:49:17 +0000282 O.flush();
283
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000284 // We didn't modify anything.
285 return false;
286}
287
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000288static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000289 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
290}
291
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000292static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
293 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
294 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
295}
296
297static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000298 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
299}
300
Chris Lattner7680e732009-06-20 19:34:09 +0000301/// print_pcrel_imm - This is used to print an immediate value that ends up
302/// being encoded as a pc-relative value. These print slightly differently, for
303/// example, a $ is not emitted.
304void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
305 const MachineOperand &MO = MI->getOperand(OpNo);
306 switch (MO.getType()) {
307 default: assert(0 && "Unknown pcrel immediate operand");
308 case MachineOperand::MO_Immediate:
309 O << MO.getImm();
310 return;
311 case MachineOperand::MO_MachineBasicBlock:
312 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
313 return;
314
315 case MachineOperand::MO_GlobalAddress: {
316 const GlobalValue *GV = MO.getGlobal();
317 std::string Name = Mang->getValueName(GV);
318 decorateName(Name, GV);
319
320 bool needCloseParen = false;
321 if (Name[0] == '$') {
322 // The name begins with a dollar-sign. In order to avoid having it look
323 // like an integer immediate to the assembler, enclose it in parens.
324 O << '(';
325 needCloseParen = true;
326 }
327
328 if (shouldPrintStub(TM, Subtarget)) {
329 // Link-once, declaration, or Weakly-linked global variables need
330 // non-lazily-resolved stubs
331 if (GV->isDeclaration() || GV->isWeakForLinker()) {
332 // Dynamically-resolved functions need a stub for the function.
333 if (isa<Function>(GV)) {
334 // Function stubs are no longer needed for Mac OS X 10.5 and up.
335 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
336 O << Name;
337 } else {
338 FnStubs.insert(Name);
339 printSuffixedName(Name, "$stub");
340 }
341 } else if (GV->hasHiddenVisibility()) {
342 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
343 // Definition is not definitely in the current translation unit.
344 O << Name;
345 else {
346 HiddenGVStubs.insert(Name);
347 printSuffixedName(Name, "$non_lazy_ptr");
348 }
349 } else {
350 GVStubs.insert(Name);
351 printSuffixedName(Name, "$non_lazy_ptr");
352 }
353 } else {
354 if (GV->hasDLLImportLinkage())
355 O << "__imp_";
356 O << Name;
357 }
358 } else {
359 if (GV->hasDLLImportLinkage()) {
360 O << "__imp_";
361 }
362 O << Name;
363
364 if (shouldPrintPLT(TM, Subtarget)) {
365 // Assemble call via PLT for externally visible symbols
366 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
367 !GV->hasLocalLinkage())
368 O << "@PLT";
369 }
370 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
371 // Save function name for later type emission
372 FnStubs.insert(Name);
373 }
374
375 if (GV->hasExternalWeakLinkage())
376 ExtWeakSymbols.insert(GV);
377
378 printOffset(MO.getOffset());
379
380 if (needCloseParen)
381 O << ')';
382 return;
383 }
384
385 case MachineOperand::MO_ExternalSymbol: {
386 bool needCloseParen = false;
387 std::string Name(TAI->getGlobalPrefix());
388 Name += MO.getSymbolName();
389 // Print function stub suffix unless it's Mac OS X 10.5 and up.
390 if (shouldPrintStub(TM, Subtarget) &&
391 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
392 FnStubs.insert(Name);
393 printSuffixedName(Name, "$stub");
394 return;
395 }
396
397 if (Name[0] == '$') {
398 // The name begins with a dollar-sign. In order to avoid having it look
399 // like an integer immediate to the assembler, enclose it in parens.
400 O << '(';
401 needCloseParen = true;
402 }
403
404 O << Name;
405
406 if (shouldPrintPLT(TM, Subtarget)) {
407 std::string GOTName(TAI->getGlobalPrefix());
408 GOTName+="_GLOBAL_OFFSET_TABLE_";
409 if (Name == GOTName)
410 // HACK! Emit extra offset to PC during printing GOT offset to
411 // compensate for the size of popl instruction. The resulting code
412 // should look like:
413 // call .piclabel
414 // piclabel:
415 // popl %some_register
416 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
417 O << " + [.-"
418 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
419
420 O << "@PLT";
421 }
422
423 if (needCloseParen)
424 O << ')';
425
426 return;
427 }
428 }
429}
430
Chris Lattnera3b8c572006-02-06 23:41:19 +0000431void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000432 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000433 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000434 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000435 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000436 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000437 "Virtual registers should not make it this far!");
438 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000439 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000440 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000441 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000442 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
443 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000444 Reg = getX86SubSuperRegister(Reg, VT);
445 }
Evan Chengae270f62008-07-07 22:21:06 +0000446 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000447 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000448 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000449
Chris Lattner63b3d712006-05-04 17:21:20 +0000450 case MachineOperand::MO_Immediate:
Evan Chenga0652002009-03-12 18:15:39 +0000451 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner7680e732009-06-20 19:34:09 +0000452 strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000453 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000454 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000455 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000456 case MachineOperand::MO_JumpTableIndex: {
457 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
458 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000459 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000460 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000461
462 if (TM.getRelocationModel() == Reloc::PIC_) {
463 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000464 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
465 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000466 else if (Subtarget->isPICStyleGOT())
467 O << "@GOTOFF";
468 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000469
Evan Chengae19abc2007-01-18 22:27:12 +0000470 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000471 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000472 return;
473 }
Evan Chenga09bd812006-02-26 08:28:12 +0000474 case MachineOperand::MO_ConstantPoolIndex: {
475 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
476 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000477 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000478 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000479
480 if (TM.getRelocationModel() == Reloc::PIC_) {
481 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000482 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
483 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000484 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000485 O << "@GOTOFF";
486 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000487
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000488 printOffset(MO.getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +0000489
Evan Chengae19abc2007-01-18 22:27:12 +0000490 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000491 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000492 return;
493 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000494 case MachineOperand::MO_GlobalAddress: {
Chris Lattneraecaa1f2009-06-21 01:27:55 +0000495 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000496 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000497
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000498 const GlobalValue *GV = MO.getGlobal();
499 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
500 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000501 // If GV is an alias then use the aliasee for determining
502 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000503 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Chris Lattneraecaa1f2009-06-21 01:27:55 +0000504 GVar =dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000505 }
506
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000507 bool isThreadLocal = GVar && GVar->isThreadLocal();
508
Evan Cheng25ab6902006-09-08 06:48:29 +0000509 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000510 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000511
Chris Lattner7680e732009-06-20 19:34:09 +0000512 if (!isMemOp)
Dan Gohman2a3250c2007-04-26 21:07:05 +0000513 O << '$';
514 else if (Name[0] == '$') {
515 // The name begins with a dollar-sign. In order to avoid having it look
516 // like an integer immediate to the assembler, enclose it in parens.
517 O << '(';
518 needCloseParen = true;
519 }
520
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000521 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000522 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000523 // non-lazily-resolved stubs
Duncan Sands667d4b82009-03-07 15:45:40 +0000524 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000525 // Dynamically-resolved functions need a stub for the function.
Chris Lattner7680e732009-06-20 19:34:09 +0000526 if (GV->hasHiddenVisibility()) {
Evan Chengae94e592008-12-05 01:06:39 +0000527 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
528 // Definition is not definitely in the current translation unit.
529 O << Name;
530 else {
531 HiddenGVStubs.insert(Name);
532 printSuffixedName(Name, "$non_lazy_ptr");
533 }
Evan Cheng2338c5c2006-02-07 08:38:37 +0000534 } else {
535 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000536 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000537 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000538 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000539 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000540 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000541 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000542 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000543
Chris Lattner7680e732009-06-20 19:34:09 +0000544 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000545 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000546 } else {
Chris Lattneraecaa1f2009-06-21 01:27:55 +0000547 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000548 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000549 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000550 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000551
Evan Cheng6d7d3102006-12-01 07:38:23 +0000552 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000553 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000554
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000555 printOffset(MO.getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +0000556
Chris Lattnere3723332009-06-21 02:22:53 +0000557 if (needCloseParen)
558 O << ')';
559
560 bool isRIPRelative = false;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000561 if (isThreadLocal) {
Rafael Espindola9a580232009-02-27 13:37:18 +0000562 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
563 switch (model) {
564 case TLSModel::GeneralDynamic:
565 O << "@TLSGD";
566 break;
567 case TLSModel::LocalDynamic:
568 // O << "@TLSLD"; // local dynamic not implemented
Bill Wendling51b16f42009-05-30 01:09:53 +0000569 O << "@TLSGD";
Rafael Espindola9a580232009-02-27 13:37:18 +0000570 break;
571 case TLSModel::InitialExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000572 if (Subtarget->is64Bit()) {
573 assert (!NotRIPRel);
Chris Lattnere3723332009-06-21 02:22:53 +0000574 O << "@GOTTPOFF";
575 isRIPRelative = true;
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000576 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +0000577 O << "@INDNTPOFF";
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000578 }
Rafael Espindola9a580232009-02-27 13:37:18 +0000579 break;
580 case TLSModel::LocalExec:
581 if (Subtarget->is64Bit())
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000582 O << "@TPOFF";
Rafael Espindola9a580232009-02-27 13:37:18 +0000583 else
Bill Wendling51b16f42009-05-30 01:09:53 +0000584 O << "@NTPOFF";
Rafael Espindola9a580232009-02-27 13:37:18 +0000585 break;
586 default:
587 assert (0 && "Unknown TLS model");
588 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000589 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000590 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000591 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000592 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000593 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000594 O << "@GOTOFF";
Chris Lattnere3723332009-06-21 02:22:53 +0000595 } else if (Subtarget->isPICStyleRIPRel() &&
596 !NotRIPRel) {
Dan Gohman72bb0a62009-03-14 02:33:41 +0000597 if (TM.getRelocationModel() != Reloc::Static) {
598 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
599 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000600 }
Chris Lattnere3723332009-06-21 02:22:53 +0000601
602 isRIPRelative = true;
Evan Chengae19abc2007-01-18 22:27:12 +0000603 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000604 }
605
Chris Lattnere3723332009-06-21 02:22:53 +0000606 // Use rip when possible to reduce code size, except when
607 // index or base register are also part of the address. e.g.
608 // foo(%rip)(%rcx,%rax,4) is not legal.
609 if (isRIPRelative)
610 O << "(%rip)";
611
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000612 return;
613 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000614 case MachineOperand::MO_ExternalSymbol: {
Dan Gohman146a3102009-04-23 00:57:37 +0000615 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000616 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000617 std::string Name(TAI->getGlobalPrefix());
618 Name += MO.getSymbolName();
Chris Lattner7680e732009-06-20 19:34:09 +0000619
Evan Cheng91a23c82008-09-20 00:13:45 +0000620 // Print function stub suffix unless it's Mac OS X 10.5 and up.
Chris Lattner7680e732009-06-20 19:34:09 +0000621 if (!isMemOp)
Dan Gohman2a3250c2007-04-26 21:07:05 +0000622 O << '$';
623 else if (Name[0] == '$') {
624 // The name begins with a dollar-sign. In order to avoid having it look
625 // like an integer immediate to the assembler, enclose it in parens.
626 O << '(';
627 needCloseParen = true;
628 }
629
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000630 O << Name;
631
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000632 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000633 std::string GOTName(TAI->getGlobalPrefix());
634 GOTName+="_GLOBAL_OFFSET_TABLE_";
635 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000636 // HACK! Emit extra offset to PC during printing GOT offset to
637 // compensate for the size of popl instruction. The resulting code
638 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000639 // call .piclabel
640 // piclabel:
641 // popl %some_register
642 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000643 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000644 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000645 }
646
Dan Gohman2a3250c2007-04-26 21:07:05 +0000647 if (needCloseParen)
648 O << ')';
649
Chris Lattner7680e732009-06-20 19:34:09 +0000650 if (Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000651 O << "(%rip)";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000652 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000653 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000654 default:
655 O << "<unknown operand type>"; return;
656 }
657}
658
Nate Begeman391c5d22005-11-30 18:54:35 +0000659void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000660 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000661 assert(value <= 7 && "Invalid ssecc argument!");
662 switch (value) {
663 case 0: O << "eq"; break;
664 case 1: O << "lt"; break;
665 case 2: O << "le"; break;
666 case 3: O << "unord"; break;
667 case 4: O << "neq"; break;
668 case 5: O << "nlt"; break;
669 case 6: O << "nle"; break;
670 case 7: O << "ord"; break;
671 }
672}
673
Rafael Espindola094fad32009-04-08 21:14:34 +0000674void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000675 const char *Modifier,
676 bool NotRIPRel) {
Chris Lattner32c9a452007-01-14 00:13:07 +0000677 MachineOperand BaseReg = MI->getOperand(Op);
678 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000679 const MachineOperand &DispSpec = MI->getOperand(Op+3);
680
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000681 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmand735b802008-10-03 15:45:36 +0000682 if (DispSpec.isGlobal() ||
683 DispSpec.isCPI() ||
Dan Gohman146a3102009-04-23 00:57:37 +0000684 DispSpec.isJTI() ||
685 DispSpec.isSymbol()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000686 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000687 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000688 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000689 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
690 O << DispVal;
691 }
692
693 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000694 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000695 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000696
Chris Lattner32c9a452007-01-14 00:13:07 +0000697 // There are cases where we can end up with ESP/RSP in the indexreg slot.
698 // If this happens, swap the base/index register to support assemblers that
699 // don't work when the index is *SP.
700 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
701 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
702 std::swap(BaseReg, IndexReg);
703 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000704 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000705
Dan Gohmand19a53b2008-06-30 22:03:41 +0000706 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000707 if (BaseReg.getReg())
708 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000709
710 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000711 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000712 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000713 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000714 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000715 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000716 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000717 }
718}
719
Rafael Espindola094fad32009-04-08 21:14:34 +0000720void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000721 const char *Modifier, bool NotRIPRel){
Rafael Espindola094fad32009-04-08 21:14:34 +0000722 assert(isMem(MI, Op) && "Invalid memory reference!");
723 MachineOperand Segment = MI->getOperand(Op+4);
724 if (Segment.getReg()) {
725 printOperand(MI, Op+4, Modifier);
726 O << ':';
727 }
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000728 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindola094fad32009-04-08 21:14:34 +0000729}
730
Anton Korobeynikov91364382008-06-28 11:08:09 +0000731void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000732 const MachineBasicBlock *MBB) const {
733 if (!TAI->getSetDirective())
734 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000735
736 // We don't need .set machinery if we have GOT-style relocations
737 if (Subtarget->isPICStyleGOT())
738 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000739
Evan Chengcc415862007-11-09 01:32:10 +0000740 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
741 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000742 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000743 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000744 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000745 << '_' << uid << '\n';
746 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000747 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000748}
749
Evan Cheng7ccced62006-02-18 00:15:05 +0000750void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000751 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000752 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000753}
754
Evan Cheng62f27002006-04-28 23:11:40 +0000755
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000756void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
757 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000758 unsigned uid) const
759{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000760 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
761 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
762
763 O << JTEntryDirective << ' ';
764
765 if (TM.getRelocationModel() == Reloc::PIC_) {
766 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
767 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
768 << '_' << uid << "_set_" << MBB->getNumber();
769 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000770 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000771 O << "@GOTOFF";
772 } else
773 assert(0 && "Don't know how to print MBB label for this PIC mode");
774 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000775 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000776}
777
Chris Lattner37710712009-06-15 04:42:32 +0000778bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000779 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000780 switch (Mode) {
781 default: return true; // Unknown mode.
782 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000783 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000784 break;
785 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000786 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000787 break;
788 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000789 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000790 break;
791 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000792 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000793 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000794 case 'q': // Print DImode register
795 Reg = getX86SubSuperRegister(Reg, MVT::i64);
796 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000797 }
798
Evan Chengae270f62008-07-07 22:21:06 +0000799 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000800 return false;
801}
802
Evan Cheng3d48a902006-04-28 21:19:05 +0000803/// PrintAsmOperand - Print out an operand for an inline asm expression.
804///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000805bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000806 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000807 const char *ExtraCode) {
808 // Does this asm operand have a single letter operand modifier?
809 if (ExtraCode && ExtraCode[0]) {
810 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000811
Evan Cheng3d48a902006-04-28 21:19:05 +0000812 switch (ExtraCode[0]) {
813 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000814 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohman72bb0a62009-03-14 02:33:41 +0000815 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattner0d924992006-10-31 20:12:30 +0000816 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000817 case 'b': // Print QImode register
818 case 'h': // Print QImode high register
819 case 'w': // Print HImode register
820 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000821 case 'q': // Print DImode register
Dan Gohmand735b802008-10-03 15:45:36 +0000822 if (MI->getOperand(OpNo).isReg())
Chris Lattner14393522007-03-25 02:01:03 +0000823 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
824 printOperand(MI, OpNo);
825 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000826
Chris Lattner7cd5e072007-03-25 01:44:57 +0000827 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000828 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000829 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000830 }
831 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000832
Evan Cheng3d48a902006-04-28 21:19:05 +0000833 printOperand(MI, OpNo);
834 return false;
835}
836
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000837bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000838 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000839 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000840 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000841 if (ExtraCode && ExtraCode[0]) {
842 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000843
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000844 switch (ExtraCode[0]) {
845 default: return true; // Unknown modifier.
846 case 'b': // Print QImode register
847 case 'h': // Print QImode high register
848 case 'w': // Print HImode register
849 case 'k': // Print SImode register
850 case 'q': // Print SImode register
851 // These only apply to registers, ignore on mem.
852 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000853 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov573c92d2009-04-28 21:49:33 +0000854 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattner91387de2009-01-23 22:33:40 +0000855 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000856 }
857 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000858 printMemReference(MI, OpNo);
859 return false;
860}
861
Chris Lattnerc1243062009-06-20 07:03:18 +0000862static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
863 // Convert registers in the addr mode according to subreg64.
864 for (unsigned i = 0; i != 4; ++i) {
865 if (!MI->getOperand(i).isReg()) continue;
866
867 unsigned Reg = MI->getOperand(i).getReg();
868 if (Reg == 0) continue;
869
870 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
871 }
872}
873
Bill Wendlingac06d002009-02-06 21:45:08 +0000874/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
875/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000876///
877void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
878 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000879
Chris Lattner475370b2009-06-19 00:47:33 +0000880 if (NewAsmPrinter) {
Chris Lattnerc1243062009-06-20 07:03:18 +0000881 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
882 O << "\t";
883 printInlineAsm(MI);
884 return;
885 } else if (MI->isLabel()) {
886 printLabel(MI);
887 return;
888 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
889 printDeclare(MI);
890 return;
891 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
892 printImplicitDef(MI);
893 return;
894 }
895
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000896 O << "NEW: ";
Chris Lattner475370b2009-06-19 00:47:33 +0000897 MCInst TmpInst;
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000898
899 TmpInst.setOpcode(MI->getOpcode());
900
901 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
902 const MachineOperand &MO = MI->getOperand(i);
903
904 MCOperand MCOp;
905 if (MO.isReg()) {
906 MCOp.MakeReg(MO.getReg());
907 } else if (MO.isImm()) {
908 MCOp.MakeImm(MO.getImm());
Chris Lattnerc1243062009-06-20 07:03:18 +0000909 } else if (MO.isMBB()) {
910 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000911 } else {
912 assert(0 && "Unimp");
913 }
914
915 TmpInst.addOperand(MCOp);
916 }
917
Chris Lattnerdc479f62009-06-20 07:59:10 +0000918 switch (TmpInst.getOpcode()) {
919 case X86::LEA64_32r:
920 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerc1243062009-06-20 07:03:18 +0000921 lower_lea64_32mem(&TmpInst, 1);
Chris Lattnerdc479f62009-06-20 07:59:10 +0000922 break;
Chris Lattnerf38c03af2009-06-20 00:49:26 +0000923 }
924
Chris Lattner475370b2009-06-19 00:47:33 +0000925 // FIXME: Convert TmpInst.
Chris Lattnerd5fb7902009-06-19 23:59:57 +0000926 printInstruction(&TmpInst);
927 O << "OLD: ";
Chris Lattner475370b2009-06-19 00:47:33 +0000928 }
929
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000930 // Call the autogenerated instruction printer routines.
931 printInstruction(MI);
932}
933
Devang Patel66b4d3b2009-06-20 01:00:07 +0000934/// doInitialization
935bool X86ATTAsmPrinter::doInitialization(Module &M) {
936 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
937 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Chris Lattner40e3c7a2009-06-24 05:46:28 +0000938
939 if (NewAsmPrinter) {
940 Context = new MCContext();
941 // FIXME: Send this to "O" instead of outs(). For now, we force it to
942 // stdout to make it easy to compare.
943 Streamer = createAsmStreamer(*Context, outs());
944 }
945
Devang Patel66b4d3b2009-06-20 01:00:07 +0000946 return AsmPrinter::doInitialization(M);
947}
948
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000949void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000950 const TargetData *TD = TM.getTargetData();
951
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000952 if (!GVar->hasInitializer())
953 return; // External global require no code
954
955 // Check to see if this is a special global used by LLVM, if so, emit it.
956 if (EmitSpecialLLVMGlobal(GVar)) {
957 if (Subtarget->isTargetDarwin() &&
958 TM.getRelocationModel() == Reloc::Static) {
959 if (GVar->getName() == "llvm.global_ctors")
960 O << ".reference .constructors_used\n";
961 else if (GVar->getName() == "llvm.global_dtors")
962 O << ".reference .destructors_used\n";
963 }
964 return;
965 }
966
967 std::string name = Mang->getValueName(GVar);
968 Constant *C = GVar->getInitializer();
969 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000970 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000971 unsigned Align = TD->getPreferredAlignmentLog(GVar);
972
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000973 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000974
975 if (Subtarget->isTargetELF())
976 O << "\t.type\t" << name << ",@object\n";
977
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000978 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000979
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000980 if (C->isNullValue() && !GVar->hasSection() &&
981 !(Subtarget->isTargetDarwin() &&
982 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000983 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000984 if (GVar->hasExternalLinkage()) {
985 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000986 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000987 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000988 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000989 return;
990 }
991 }
992
993 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000994 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000995 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000996
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000997 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000998 if (GVar->hasLocalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000999 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001000 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +00001001 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001002 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +00001003 O << "\t.globl " << name << '\n'
1004 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001005 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001006 O << name << ":";
1007 if (VerboseAsm) {
Evan Cheng7db860d2009-03-25 01:08:42 +00001008 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +00001009 PrintUnmangledNameSafely(GVar, O);
1010 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001011 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001012 EmitGlobalConstant(C);
1013 return;
1014 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +00001015 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16b7f512008-08-08 18:25:52 +00001016 if (TAI->getCOMMDirectiveTakesAlignment())
1017 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001018 }
1019 } else {
1020 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001021 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +00001022 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001023 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001024 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001025 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +00001026 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001027 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001028 if (VerboseAsm) {
1029 O << "\t\t" << TAI->getCommentString() << ' ';
1030 PrintUnmangledNameSafely(GVar, O);
1031 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001032 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001033 return;
1034 }
1035 }
1036
1037 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +00001038 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00001039 case GlobalValue::LinkOnceAnyLinkage:
1040 case GlobalValue::LinkOnceODRLinkage:
1041 case GlobalValue::WeakAnyLinkage:
1042 case GlobalValue::WeakODRLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001043 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +00001044 O << "\t.globl " << name << '\n'
1045 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001046 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001047 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +00001048 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001049 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +00001050 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001051 }
1052 break;
Evan Cheng15621a22008-08-08 06:43:59 +00001053 case GlobalValue::DLLExportLinkage:
1054 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001055 // FIXME: appending linkage variables should go into a section of
1056 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +00001057 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001058 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +00001059 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001060 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +00001061 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +00001062 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001063 break;
Evan Cheng15621a22008-08-08 06:43:59 +00001064 default:
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001065 assert(0 && "Unknown linkage type!");
1066 }
1067
1068 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001069 O << name << ":";
1070 if (VerboseAsm){
Evan Cheng7db860d2009-03-25 01:08:42 +00001071 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Chengf1c0ae92009-03-24 00:17:40 +00001072 PrintUnmangledNameSafely(GVar, O);
1073 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001074 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001075 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +00001076 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001077
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001078 EmitGlobalConstant(C);
1079}
1080
Evan Cheng77c8f762008-07-08 00:55:58 +00001081/// printGVStub - Print stub for a global value.
1082///
1083void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +00001084 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +00001085 O << ":\n\t.indirect_symbol ";
1086 if (Prefix) O << Prefix;
1087 O << GV << "\n\t.long\t0\n";
1088}
1089
Evan Chengae94e592008-12-05 01:06:39 +00001090/// printHiddenGVStub - Print stub for a hidden global value.
1091///
1092void X86ATTAsmPrinter::printHiddenGVStub(const char *GV, const char *Prefix) {
1093 EmitAlignment(2);
1094 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
1095 if (Prefix) O << Prefix;
1096 O << ":\n" << TAI->getData32bitsDirective() << GV << '\n';
1097}
1098
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001099
1100bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001101 // Print out module-level global variables here.
1102 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +00001103 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +00001104 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001105
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +00001106 if (I->hasDLLExportLinkage())
1107 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +00001108
1109 // If the global is a extern weak symbol, remember to emit the weak
1110 // reference!
Mike Stump6726be62009-05-14 23:22:47 +00001111 // FIXME: This is rather hacky, since we'll emit references to ALL weak
1112 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +00001113 // initializers hidden deep inside constant expressions.
1114 if (I->hasExternalWeakLinkage())
1115 ExtWeakSymbols.insert(I);
1116 }
1117
1118 for (Module::const_iterator I = M.begin(), E = M.end();
1119 I != E; ++I) {
1120 // If the global is a extern weak symbol, remember to emit the weak
1121 // reference!
Mike Stump11adeed2009-05-14 23:23:37 +00001122 // FIXME: This is rather hacky, since we'll emit references to ALL weak
1123 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +00001124 // initializers hidden deep inside constant expressions.
1125 if (I->hasExternalWeakLinkage())
1126 ExtWeakSymbols.insert(I);
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +00001127 }
1128
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001129 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001130 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001131 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001132
1133 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1134 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001135 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001136 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001137
1138 if (!DLLExportedFns.empty()) {
1139 SwitchToDataSection(".section .drectve");
1140 }
1141
1142 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1143 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001144 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001145 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001146
1147 if (Subtarget->isTargetDarwin()) {
1148 SwitchToDataSection("");
1149
1150 // Output stubs for dynamically-linked functions
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001151 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
Evan Chengae94e592008-12-05 01:06:39 +00001152 i != e; ++i) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001153 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1154 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +00001155 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001156 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +00001157 O << ":\n"
1158 "\t.indirect_symbol " << p << "\n"
1159 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001160 }
1161
Dan Gohmand19a53b2008-06-30 22:03:41 +00001162 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001163
Evan Cheng77c8f762008-07-08 00:55:58 +00001164 // Print global value stubs.
1165 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001166 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1167 // Add the (possibly multiple) personalities to the set of global values.
1168 // Only referenced functions get into the Personalities list.
1169 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001170 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +00001171 E = Personalities.end(); I != E; ++I) {
1172 if (!*I)
1173 continue;
1174 if (!InStubSection) {
1175 SwitchToDataSection(
1176 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1177 InStubSection = true;
1178 }
1179 printGVStub((*I)->getNameStart(), "_");
1180 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001181 }
1182
1183 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +00001184 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001185 SwitchToDataSection(
1186 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1187 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +00001188 i != e; ++i)
1189 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001190
Evan Chengae94e592008-12-05 01:06:39 +00001191 if (!HiddenGVStubs.empty()) {
1192 SwitchToSection(TAI->getDataSection());
1193 for (StringSet<>::iterator i = HiddenGVStubs.begin(), e = HiddenGVStubs.end();
1194 i != e; ++i)
1195 printHiddenGVStub(i->getKeyData());
1196 }
1197
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001198 // Emit final debug information.
Devang Patel5090f192009-06-19 23:21:20 +00001199 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel14a55d92009-06-19 21:54:26 +00001200 DW->EndModule();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001201
1202 // Funny Darwin hack: This flag tells the linker that no global symbols
1203 // contain code that falls through to other global symbols (e.g. the obvious
1204 // implementation of multiple entry points). If this doesn't occur, the
1205 // linker can safely perform dead code stripping. Since LLVM never
1206 // generates code that does this, it is always safe to set.
1207 O << "\t.subsections_via_symbols\n";
1208 } else if (Subtarget->isTargetCygMing()) {
1209 // Emit type information for external functions
1210 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1211 i != e; ++i) {
1212 O << "\t.def\t " << i->getKeyData()
1213 << ";\t.scl\t" << COFF::C_EXT
1214 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1215 << ";\t.endef\n";
1216 }
1217
1218 // Emit final debug information.
Devang Patel5090f192009-06-19 23:21:20 +00001219 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel14a55d92009-06-19 21:54:26 +00001220 DW->EndModule();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001221 } else if (Subtarget->isTargetELF()) {
1222 // Emit final debug information.
Devang Patel5090f192009-06-19 23:21:20 +00001223 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patel14a55d92009-06-19 21:54:26 +00001224 DW->EndModule();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001225 }
1226
Chris Lattner40e3c7a2009-06-24 05:46:28 +00001227 if (NewAsmPrinter) {
1228 Streamer->Finish();
1229
1230 delete Streamer;
1231 delete Context;
1232 Streamer = 0;
1233 Context = 0;
1234 }
1235
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001236 return AsmPrinter::doFinalization(M);
1237}
1238
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001239// Include the auto-generated portion of the assembly writer.
1240#include "X86GenAsmWriter.inc"