blob: 9eabfc0bd3fa723c5ccb7f70819c121b1615cf39 [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 Lattner7cf8daf2009-06-24 05:46:28 +000029#include "llvm/MC/MCContext.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000030#include "llvm/MC/MCInst.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000031#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000032#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000034#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000036#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetAsmInfo.h"
38#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039using namespace llvm;
40
41STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
Chris Lattner19b1bd52009-06-19 00:47:33 +000043static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
44 cl::Hidden);
45
Evan Cheng0729ccf2008-01-05 00:41:47 +000046static std::string getPICLabelString(unsigned FnNum,
47 const TargetAsmInfo *TAI,
48 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 std::string label;
50 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000051 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000053 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 else
55 assert(0 && "Don't know how to print PIC label!\n");
56
57 return label;
58}
59
Anton Korobeynikovab6c6a42008-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 Pateld222f862008-09-25 21:00:45 +000082 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000083 Ty = cast<PointerType>(Ty)->getElementType();
84
85 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000086 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-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 Gohman8387bb32009-03-03 02:55:14 +000095/// Don't print things like \\n or \\0.
Owen Anderson847b99b2008-08-21 00:14:44 +000096static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-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 Korobeynikov30948e32008-06-28 11:09:01 +0000157void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000160 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000162 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000163
Devang Patel93698d92008-10-01 23:18:38 +0000164 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000165 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000166 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 switch (F->getLinkage()) {
168 default: assert(0 && "Unknown linkage type!");
169 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000170 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000171 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 break;
173 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000178 case Function::LinkOnceAnyLinkage:
179 case Function::LinkOnceODRLinkage:
180 case Function::WeakAnyLinkage:
181 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000182 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000184 O << "\t.globl\t" << CurrentFnName << '\n';
185 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << CurrentFnName << "\n"
188 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000190 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 }
192 break;
193 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000194
195 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
197 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000198 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 else if (Subtarget->isTargetCygMing()) {
200 O << "\t.def\t " << CurrentFnName
201 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000202 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
204 << ";\t.endef\n";
205 }
206
207 O << CurrentFnName << ":\n";
208 // Add some workaround for linkonce linkage on Cygwin\MinGW
209 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000210 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000219 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000220 unsigned CC = F->getCallingConv();
221
Anton Korobeynikov30948e32008-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 Patelaa1e8432009-01-08 23:40:34 +0000241 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000244 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246 I != E; ++I) {
247 // Print a label for the basic block.
Dan Gohmand38f8762009-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 Cheng11db8142009-03-24 00:17:40 +0000252 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 O << '\n';
254 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000255 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
256 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000258 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000259 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 printMachineInstruction(II);
261 }
262 }
263
Dale Johannesenf35771f2008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000273 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000275 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000276 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000277 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
279 // Print out jump tables referenced by the function.
280 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000281
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000282 O.flush();
283
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 // We didn't modify anything.
285 return false;
286}
287
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000288static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
290}
291
Rafael Espindolae0ac18d2008-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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
299}
300
Chris Lattner357a0ca2009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
432 const char *Modifier, bool NotRIPRel) {
433 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 switch (MO.getType()) {
435 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000436 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 "Virtual registers should not make it this far!");
438 O << '%';
439 unsigned Reg = MO.getReg();
440 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000441 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
443 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
444 Reg = getX86SubSuperRegister(Reg, VT);
445 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000446 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 return;
448 }
449
450 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000451 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000452 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000454 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 case MachineOperand::MO_JumpTableIndex: {
457 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
458 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000459 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000460 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461
462 if (TM.getRelocationModel() == Reloc::PIC_) {
463 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000464 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
465 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 else if (Subtarget->isPICStyleGOT())
467 O << "@GOTOFF";
468 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
471 O << "(%rip)";
472 return;
473 }
474 case MachineOperand::MO_ConstantPoolIndex: {
475 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
476 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000477 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000478 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479
480 if (TM.getRelocationModel() == Reloc::PIC_) {
481 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000482 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
483 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 else if (Subtarget->isPICStyleGOT())
485 O << "@GOTOFF";
486 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000487
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000488 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489
490 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
491 O << "(%rip)";
492 return;
493 }
494 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000495 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 bool needCloseParen = false;
497
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000498 const GlobalValue *GV = MO.getGlobal();
499 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
500 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000501 // If GV is an alias then use the aliasee for determining
502 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000503 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000504 GVar =dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000505 }
506
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 bool isThreadLocal = GVar && GVar->isThreadLocal();
508
509 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000510 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000511
Chris Lattner357a0ca2009-06-20 19:34:09 +0000512 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Espindolae0ac18d2008-06-09 09:52:31 +0000521 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 // Link-once, declaration, or Weakly-linked global variables need
523 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000524 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000526 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 } else {
535 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000536 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 }
538 } else {
539 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000540 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 O << Name;
542 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000543
Chris Lattner357a0ca2009-06-20 19:34:09 +0000544 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000545 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000547 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000548 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 }
551
552 if (GV->hasExternalWeakLinkage())
553 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000554
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000555 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556
Chris Lattnerdabfe572009-06-21 02:22:53 +0000557 if (needCloseParen)
558 O << ')';
559
560 bool isRIPRelative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 if (isThreadLocal) {
Rafael Espindola7b620af2009-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 Wendlingc1946742009-05-30 01:09:53 +0000569 O << "@TLSGD";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000570 break;
571 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000572 if (Subtarget->is64Bit()) {
573 assert (!NotRIPRel);
Chris Lattnerdabfe572009-06-21 02:22:53 +0000574 O << "@GOTTPOFF";
575 isRIPRelative = true;
Rafael Espindolab93a5122009-04-13 13:02:49 +0000576 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000577 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000578 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000579 break;
580 case TLSModel::LocalExec:
581 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000582 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000583 else
Bill Wendlingc1946742009-05-30 01:09:53 +0000584 O << "@NTPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000585 break;
586 default:
587 assert (0 && "Unknown TLS model");
588 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000590 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
592 O << "@GOT";
593 else
594 O << "@GOTOFF";
Chris Lattnerdabfe572009-06-21 02:22:53 +0000595 } else if (Subtarget->isPICStyleRIPRel() &&
596 !NotRIPRel) {
Dan Gohmane254f322009-03-14 02:33:41 +0000597 if (TM.getRelocationModel() != Reloc::Static) {
598 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
599 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 }
Chris Lattnerdabfe572009-06-21 02:22:53 +0000601
602 isRIPRelative = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 }
604 }
605
Chris Lattnerdabfe572009-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 return;
613 }
614 case MachineOperand::MO_ExternalSymbol: {
Dan Gohman96f096e2009-04-23 00:57:37 +0000615 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 bool needCloseParen = false;
617 std::string Name(TAI->getGlobalPrefix());
618 Name += MO.getSymbolName();
Chris Lattner357a0ca2009-06-20 19:34:09 +0000619
Evan Chengdfd884e2008-09-20 00:13:45 +0000620 // Print function stub suffix unless it's Mac OS X 10.5 and up.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000621 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
630 O << Name;
631
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000632 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 std::string GOTName(TAI->getGlobalPrefix());
634 GOTName+="_GLOBAL_OFFSET_TABLE_";
635 if (Name == GOTName)
636 // 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:
639 // call .piclabel
640 // piclabel:
641 // popl %some_register
642 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
643 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000644 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 }
646
647 if (needCloseParen)
648 O << ')';
649
Chris Lattner357a0ca2009-06-20 19:34:09 +0000650 if (Subtarget->isPICStyleRIPRel())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 O << "(%rip)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 return;
653 }
654 default:
655 O << "<unknown operand type>"; return;
656 }
657}
658
659void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000660 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Espindolabca99f72009-04-08 21:14:34 +0000674void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000675 const char *Modifier,
676 bool NotRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 MachineOperand BaseReg = MI->getOperand(Op);
678 MachineOperand IndexReg = MI->getOperand(Op+2);
679 const MachineOperand &DispSpec = MI->getOperand(Op+3);
680
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000681 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000682 if (DispSpec.isGlobal() ||
683 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000684 DispSpec.isJTI() ||
685 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 printOperand(MI, Op+3, "mem", NotRIPRel);
687 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000688 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
690 O << DispVal;
691 }
692
693 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000694 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000696
Dan Gohmanf17a25c2007-07-18 16:29:46 +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);
704 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000705
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000706 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 if (BaseReg.getReg())
708 printOperand(MI, Op+BaseRegOperand, Modifier);
709
710 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000711 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 printOperand(MI, Op+IndexRegOperand, Modifier);
713 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000714 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000716 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 }
718}
719
Rafael Espindolabca99f72009-04-08 21:14:34 +0000720void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000721 const char *Modifier, bool NotRIPRel){
Rafael Espindolabca99f72009-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 Korobeynikov0cdf2462009-04-28 21:49:33 +0000728 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000729}
730
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000731void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000732 const MachineBasicBlock *MBB) const {
733 if (!TAI->getSetDirective())
734 return;
Anton Korobeynikov5772c672007-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 Korobeynikovd97b85e2008-06-28 11:08:09 +0000739
Evan Cheng6fb06762007-11-09 01:32:10 +0000740 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
741 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000742 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000743 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000744 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000745 << '_' << uid << '\n';
746 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000747 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000748}
749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000751 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000752 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753}
754
755
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000756void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
757 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000758 unsigned uid) const
759{
Anton Korobeynikov5772c672007-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 Cheng45c1edb2008-02-28 00:43:03 +0000770 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-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 Cheng45c1edb2008-02-28 00:43:03 +0000775 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000776}
777
Chris Lattner8bb96e42009-06-15 04:42:32 +0000778bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 unsigned Reg = MO.getReg();
780 switch (Mode) {
781 default: return true; // Unknown mode.
782 case 'b': // Print QImode register
783 Reg = getX86SubSuperRegister(Reg, MVT::i8);
784 break;
785 case 'h': // Print QImode high register
786 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
787 break;
788 case 'w': // Print HImode register
789 Reg = getX86SubSuperRegister(Reg, MVT::i16);
790 break;
791 case 'k': // Print SImode register
792 Reg = getX86SubSuperRegister(Reg, MVT::i32);
793 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000794 case 'q': // Print DImode register
795 Reg = getX86SubSuperRegister(Reg, MVT::i64);
796 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 }
798
Evan Cheng00d04a72008-07-07 22:21:06 +0000799 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 return false;
801}
802
803/// PrintAsmOperand - Print out an operand for an inline asm expression.
804///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000805bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000806 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikovd97b85e2008-06-28 11:08:09 +0000811
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 switch (ExtraCode[0]) {
813 default: return true; // Unknown modifier.
814 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000815 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 return false;
817 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 Lattner1fabfaa2007-10-29 03:09:07 +0000821 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000822 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
824 printOperand(MI, OpNo);
825 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000826
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000828 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 return false;
830 }
831 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000832
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 printOperand(MI, OpNo);
834 return false;
835}
836
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000837bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000839 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000841 if (ExtraCode && ExtraCode[0]) {
842 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000843
Chris Lattner1fabfaa2007-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 Lattnerd1595722009-01-23 22:33:40 +0000853 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000854 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattnerd1595722009-01-23 22:33:40 +0000855 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000856 }
857 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 printMemReference(MI, OpNo);
859 return false;
860}
861
Chris Lattnerf5da5902009-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 Wendlingb3b11262009-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876///
877void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
878 ++EmittedInsts;
879
Chris Lattner19b1bd52009-06-19 00:47:33 +0000880 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-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 Lattnere67184e2009-06-19 23:59:57 +0000896 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000897 MCInst TmpInst;
Chris Lattner4ebc52f2009-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 Lattnerf5da5902009-06-20 07:03:18 +0000909 } else if (MO.isMBB()) {
910 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000911 } else {
912 assert(0 && "Unimp");
913 }
914
915 TmpInst.addOperand(MCOp);
916 }
917
Chris Lattner6b9f7742009-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 Lattnerf5da5902009-06-20 07:03:18 +0000921 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000922 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000923 }
924
Chris Lattner19b1bd52009-06-19 00:47:33 +0000925 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000926 printInstruction(&TmpInst);
927 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000928 }
929
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 // Call the autogenerated instruction printer routines.
931 printInstruction(MI);
932}
933
Devang Patelc20079e2009-06-20 01:00:07 +0000934/// doInitialization
935bool X86ATTAsmPrinter::doInitialization(Module &M) {
936 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
937 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Chris Lattner7cf8daf2009-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 Patelc20079e2009-06-20 01:00:07 +0000946 return AsmPrinter::doInitialization(M);
947}
948
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000949void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000950 const TargetData *TD = TM.getTargetData();
951
Anton Korobeynikovfc3efd82008-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 Sandsec4f97d2009-05-09 07:06:46 +0000970 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000971 unsigned Align = TD->getPreferredAlignmentLog(GVar);
972
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000973 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000974
975 if (Subtarget->isTargetELF())
976 O << "\t.type\t" << name << ",@object\n";
977
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000978 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000979
Evan Chengcf84b142009-02-18 02:19:52 +0000980 if (C->isNullValue() && !GVar->hasSection() &&
981 !(Subtarget->isTargetDarwin() &&
982 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000983 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000984 if (GVar->hasExternalLinkage()) {
985 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000986 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000987 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000988 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000989 return;
990 }
991 }
992
993 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000994 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000995 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000996
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000997 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000998 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000999 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001000 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001001 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001002 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001003 O << "\t.globl " << name << '\n'
1004 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001005 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001006 O << name << ":";
1007 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +00001008 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001009 PrintUnmangledNameSafely(GVar, O);
1010 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001011 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001012 EmitGlobalConstant(C);
1013 return;
1014 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001015 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +00001016 if (TAI->getCOMMDirectiveTakesAlignment())
1017 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001018 }
1019 } else {
1020 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001021 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001022 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001023 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001024 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001025 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001026 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001027 }
Evan Cheng11db8142009-03-24 00:17:40 +00001028 if (VerboseAsm) {
1029 O << "\t\t" << TAI->getCommentString() << ' ';
1030 PrintUnmangledNameSafely(GVar, O);
1031 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001032 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001033 return;
1034 }
1035 }
1036
1037 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001038 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001039 case GlobalValue::LinkOnceAnyLinkage:
1040 case GlobalValue::LinkOnceODRLinkage:
1041 case GlobalValue::WeakAnyLinkage:
1042 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001043 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001044 O << "\t.globl " << name << '\n'
1045 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001046 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001047 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001048 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001049 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001050 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001051 }
1052 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001053 case GlobalValue::DLLExportLinkage:
1054 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-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 Cheng630c5612008-08-08 06:43:59 +00001057 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001058 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001059 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001060 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001061 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001062 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001063 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001064 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001065 assert(0 && "Unknown linkage type!");
1066 }
1067
1068 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001069 O << name << ":";
1070 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +00001071 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001072 PrintUnmangledNameSafely(GVar, O);
1073 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001074 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001075 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001076 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001077
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001078 EmitGlobalConstant(C);
1079}
1080
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001081bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001082 // Print out module-level global variables here.
1083 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001084 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001085 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001086
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001087 if (I->hasDLLExportLinkage())
1088 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001089
1090 // If the global is a extern weak symbol, remember to emit the weak
1091 // reference!
Mike Stump5dae8792009-05-14 23:22:47 +00001092 // FIXME: This is rather hacky, since we'll emit references to ALL weak
1093 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001094 // initializers hidden deep inside constant expressions.
1095 if (I->hasExternalWeakLinkage())
1096 ExtWeakSymbols.insert(I);
1097 }
1098
1099 for (Module::const_iterator I = M.begin(), E = M.end();
1100 I != E; ++I) {
1101 // If the global is a extern weak symbol, remember to emit the weak
1102 // reference!
Mike Stump809b1092009-05-14 23:23:37 +00001103 // FIXME: This is rather hacky, since we'll emit references to ALL weak
1104 // stuff, not used. But currently it's the only way to deal with extern weak
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001105 // initializers hidden deep inside constant expressions.
1106 if (I->hasExternalWeakLinkage())
1107 ExtWeakSymbols.insert(I);
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001108 }
1109
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001110 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001111 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001112 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001113
1114 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1115 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001116 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001117 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001118
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001119 if (!DLLExportedFns.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001120 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001121
1122 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1123 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +00001124 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001125 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001126
1127 if (Subtarget->isTargetDarwin()) {
1128 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +00001129
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001130 // Add the (possibly multiple) personalities to the set of global value
1131 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001132 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001133 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1134 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
1135 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +00001136 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001137 std::string Name = Mang->getValueName(Personalities[i]);
1138 decorateName(Name, Personalities[i]);
1139 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +00001140 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001141 }
1142
Chris Lattner2e860262009-06-24 18:24:09 +00001143 // Output stubs for dynamically-linked functions
1144 if (!FnStubs.empty()) {
1145 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
1146 I != E; ++I) {
1147 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1148 "self_modifying_code+pure_instructions,5", 0);
1149 const char *Name = I->getKeyData();
1150 printSuffixedName(Name, "$stub");
1151 O << ":\n"
1152 "\t.indirect_symbol " << Name << "\n"
1153 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1154 }
1155 O << '\n';
1156 }
1157
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001158 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001159 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001160 SwitchToDataSection(
1161 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001162 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1163 I != E; ++I) {
1164 const char *Name = I->getKeyData();
1165 printSuffixedName(Name, "$non_lazy_ptr");
1166 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1167 }
1168 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001169
Evan Chenga65854f2008-12-05 01:06:39 +00001170 if (!HiddenGVStubs.empty()) {
1171 SwitchToSection(TAI->getDataSection());
Chris Lattner2e860262009-06-24 18:24:09 +00001172 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1173 E = HiddenGVStubs.end(); I != E; ++I) {
1174 EmitAlignment(2);
1175 const char *Name = I->getKeyData();
1176 printSuffixedName(Name, "$non_lazy_ptr");
1177 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1178 }
Evan Chenga65854f2008-12-05 01:06:39 +00001179 }
1180
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001181 // Funny Darwin hack: This flag tells the linker that no global symbols
1182 // contain code that falls through to other global symbols (e.g. the obvious
1183 // implementation of multiple entry points). If this doesn't occur, the
1184 // linker can safely perform dead code stripping. Since LLVM never
1185 // generates code that does this, it is always safe to set.
1186 O << "\t.subsections_via_symbols\n";
1187 } else if (Subtarget->isTargetCygMing()) {
1188 // Emit type information for external functions
1189 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1190 i != e; ++i) {
1191 O << "\t.def\t " << i->getKeyData()
1192 << ";\t.scl\t" << COFF::C_EXT
1193 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1194 << ";\t.endef\n";
1195 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001196 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001197
1198 // Emit final debug information.
1199 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
1200 DW->EndModule();
1201
1202
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001203 if (NewAsmPrinter) {
1204 Streamer->Finish();
1205
1206 delete Streamer;
1207 delete Context;
1208 Streamer = 0;
1209 Context = 0;
1210 }
1211
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001212 return AsmPrinter::doFinalization(M);
1213}
1214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001215// Include the auto-generated portion of the assembly writer.
1216#include "X86GenAsmWriter.inc"