blob: 565f09fa76b295f3fba33904cf4b15714fde6fbe [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"
Devang Patelf667ab42009-06-25 00:47:42 +000026#include "llvm/MDNode.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000027#include "llvm/Type.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000030#include "llvm/MC/MCContext.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000031#include "llvm/MC/MCInst.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000032#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000033#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000035#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000037#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetAsmInfo.h"
39#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040using namespace llvm;
41
42STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
Chris Lattner19b1bd52009-06-19 00:47:33 +000044static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
45 cl::Hidden);
46
Chris Lattner2e845952009-06-24 19:44:36 +000047//===----------------------------------------------------------------------===//
48// Primitive Helper Functions.
49//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000050
51void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000053 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 else if (Subtarget->isTargetELF())
Chris Lattner14f791a2009-06-24 19:19:16 +000055 O << ".Lllvm$" << getFunctionNumber() << "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 else
57 assert(0 && "Don't know how to print PIC label!\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058}
59
Chris Lattner2e845952009-06-24 19:44:36 +000060/// PrintUnmangledNameSafely - Print out the printable characters in the name.
61/// Don't print things like \\n or \\0.
62static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
63 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
64 Name != E; ++Name)
65 if (isprint(*Name))
66 OS << *Name;
67}
Chris Lattner14f791a2009-06-24 19:19:16 +000068
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000069static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
70 const TargetData *TD) {
71 X86MachineFunctionInfo Info;
72 uint64_t Size = 0;
73
74 switch (F->getCallingConv()) {
75 case CallingConv::X86_StdCall:
76 Info.setDecorationStyle(StdCall);
77 break;
78 case CallingConv::X86_FastCall:
79 Info.setDecorationStyle(FastCall);
80 break;
81 default:
82 return Info;
83 }
84
85 unsigned argNum = 1;
86 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
87 AI != AE; ++AI, ++argNum) {
88 const Type* Ty = AI->getType();
89
90 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000091 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000092 Ty = cast<PointerType>(Ty)->getElementType();
93
94 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000095 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000096 }
97
98 // We're not supporting tooooo huge arguments :)
99 Info.setBytesToPopOnReturn((unsigned int)Size);
100 return Info;
101}
102
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000103/// 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
Chris Lattner2e845952009-06-24 19:44:36 +0000157
158
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000159void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000162 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000164 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000165
Chris Lattner2e845952009-06-24 19:44:36 +0000166 // FIXME: A function's alignment should be part of MachineFunction. There
167 // shouldn't be a policy decision here.
Devang Patel93698d92008-10-01 23:18:38 +0000168 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000169 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000170 FnAlign = 1;
Chris Lattner2e845952009-06-24 19:44:36 +0000171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 switch (F->getLinkage()) {
173 default: assert(0 && "Unknown linkage type!");
174 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000175 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000176 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
178 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000180 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000183 case Function::LinkOnceAnyLinkage:
184 case Function::LinkOnceODRLinkage:
185 case Function::WeakAnyLinkage:
186 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000187 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000189 O << "\t.globl\t" << CurrentFnName << '\n';
190 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000192 O << "\t.globl\t" << CurrentFnName << "\n"
193 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000195 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 }
197 break;
198 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000199
200 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
202 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000203 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 else if (Subtarget->isTargetCygMing()) {
205 O << "\t.def\t " << CurrentFnName
206 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000207 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
209 << ";\t.endef\n";
210 }
211
212 O << CurrentFnName << ":\n";
213 // Add some workaround for linkonce linkage on Cygwin\MinGW
214 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000215 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000217}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000219/// runOnMachineFunction - This uses the printMachineInstruction()
220/// method to print assembly for each instruction.
221///
222bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
223 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000224 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000225 unsigned CC = F->getCallingConv();
226
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000227 SetupMachineFunction(MF);
228 O << "\n\n";
229
230 // Populate function information map. Actually, We don't want to populate
231 // non-stdcall or non-fastcall functions' information right now.
232 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
233 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
234
235 // Print out constants referenced by the function
236 EmitConstantPool(MF.getConstantPool());
237
238 if (F->hasDLLExportLinkage())
239 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
240
241 // Print the 'header' of function
242 emitFunctionHeader(MF);
243
244 // Emit pre-function debug and/or EH information.
245 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000246 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000249 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
251 I != E; ++I) {
252 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000253 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
254 // This is an entry block or a block that's only reachable via a
255 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
256 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000257 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 O << '\n';
259 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000260 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
261 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000263 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000264 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 printMachineInstruction(II);
266 }
267 }
268
Dale Johannesenf35771f2008-04-08 00:37:56 +0000269 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
270 // If the function is empty, then we need to emit *something*. Otherwise,
271 // the function's label might be associated with something that it wasn't
272 // meant to be associated with. We emit a noop in this situation.
273 // We are assuming inline asms are code.
274 O << "\tnop\n";
275 }
276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000278 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000280 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000281 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000282 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
284 // Print out jump tables referenced by the function.
285 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000286
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000287 O.flush();
288
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 // We didn't modify anything.
290 return false;
291}
292
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000293static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
295}
296
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000297static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
298 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
299 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
300}
301
302static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
304}
305
Chris Lattner357a0ca2009-06-20 19:34:09 +0000306/// print_pcrel_imm - This is used to print an immediate value that ends up
307/// being encoded as a pc-relative value. These print slightly differently, for
308/// example, a $ is not emitted.
309void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
310 const MachineOperand &MO = MI->getOperand(OpNo);
311 switch (MO.getType()) {
312 default: assert(0 && "Unknown pcrel immediate operand");
313 case MachineOperand::MO_Immediate:
314 O << MO.getImm();
315 return;
316 case MachineOperand::MO_MachineBasicBlock:
317 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
318 return;
319
320 case MachineOperand::MO_GlobalAddress: {
321 const GlobalValue *GV = MO.getGlobal();
322 std::string Name = Mang->getValueName(GV);
323 decorateName(Name, GV);
324
325 bool needCloseParen = false;
326 if (Name[0] == '$') {
327 // The name begins with a dollar-sign. In order to avoid having it look
328 // like an integer immediate to the assembler, enclose it in parens.
329 O << '(';
330 needCloseParen = true;
331 }
332
333 if (shouldPrintStub(TM, Subtarget)) {
334 // Link-once, declaration, or Weakly-linked global variables need
335 // non-lazily-resolved stubs
336 if (GV->isDeclaration() || GV->isWeakForLinker()) {
337 // Dynamically-resolved functions need a stub for the function.
338 if (isa<Function>(GV)) {
339 // Function stubs are no longer needed for Mac OS X 10.5 and up.
340 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
341 O << Name;
342 } else {
343 FnStubs.insert(Name);
344 printSuffixedName(Name, "$stub");
345 }
346 } else if (GV->hasHiddenVisibility()) {
347 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
348 // Definition is not definitely in the current translation unit.
349 O << Name;
350 else {
351 HiddenGVStubs.insert(Name);
352 printSuffixedName(Name, "$non_lazy_ptr");
353 }
354 } else {
355 GVStubs.insert(Name);
356 printSuffixedName(Name, "$non_lazy_ptr");
357 }
358 } else {
359 if (GV->hasDLLImportLinkage())
360 O << "__imp_";
361 O << Name;
362 }
363 } else {
364 if (GV->hasDLLImportLinkage()) {
365 O << "__imp_";
366 }
367 O << Name;
368
369 if (shouldPrintPLT(TM, Subtarget)) {
370 // Assemble call via PLT for externally visible symbols
371 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
372 !GV->hasLocalLinkage())
373 O << "@PLT";
374 }
375 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
376 // Save function name for later type emission
377 FnStubs.insert(Name);
378 }
379
Chris Lattner357a0ca2009-06-20 19:34:09 +0000380 printOffset(MO.getOffset());
381
382 if (needCloseParen)
383 O << ')';
384 return;
385 }
386
387 case MachineOperand::MO_ExternalSymbol: {
388 bool needCloseParen = false;
389 std::string Name(TAI->getGlobalPrefix());
390 Name += MO.getSymbolName();
391 // Print function stub suffix unless it's Mac OS X 10.5 and up.
392 if (shouldPrintStub(TM, Subtarget) &&
393 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
394 FnStubs.insert(Name);
395 printSuffixedName(Name, "$stub");
396 return;
397 }
398
399 if (Name[0] == '$') {
400 // The name begins with a dollar-sign. In order to avoid having it look
401 // like an integer immediate to the assembler, enclose it in parens.
402 O << '(';
403 needCloseParen = true;
404 }
405
406 O << Name;
407
408 if (shouldPrintPLT(TM, Subtarget)) {
409 std::string GOTName(TAI->getGlobalPrefix());
410 GOTName+="_GLOBAL_OFFSET_TABLE_";
Chris Lattner14f791a2009-06-24 19:19:16 +0000411 if (Name == GOTName) {
Chris Lattner357a0ca2009-06-20 19:34:09 +0000412 // HACK! Emit extra offset to PC during printing GOT offset to
413 // compensate for the size of popl instruction. The resulting code
414 // should look like:
415 // call .piclabel
416 // piclabel:
417 // popl %some_register
418 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Chris Lattner14f791a2009-06-24 19:19:16 +0000419 O << " + [.-";
420 PrintPICBaseSymbol();
421 O << ']';
422 }
Chris Lattner357a0ca2009-06-20 19:34:09 +0000423
424 O << "@PLT";
425 }
426
427 if (needCloseParen)
428 O << ')';
429
430 return;
431 }
432 }
433}
434
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
436 const char *Modifier, bool NotRIPRel) {
437 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 switch (MO.getType()) {
439 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000440 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 "Virtual registers should not make it this far!");
442 O << '%';
443 unsigned Reg = MO.getReg();
444 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000445 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
447 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
448 Reg = getX86SubSuperRegister(Reg, VT);
449 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000450 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 return;
452 }
453
454 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000455 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000456 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000458 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 case MachineOperand::MO_JumpTableIndex: {
461 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
462 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000463 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000464 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465
466 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2e845952009-06-24 19:44:36 +0000467 if (Subtarget->isPICStyleStub()) {
468 O << '-';
469 PrintPICBaseSymbol();
470 } else if (Subtarget->isPICStyleGOT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 O << "@GOTOFF";
Chris Lattner2e845952009-06-24 19:44:36 +0000472 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000474
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
476 O << "(%rip)";
477 return;
478 }
479 case MachineOperand::MO_ConstantPoolIndex: {
480 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
481 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000482 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000483 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484
485 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2e845952009-06-24 19:44:36 +0000486 if (Subtarget->isPICStyleStub()) {
487 O << '-';
488 PrintPICBaseSymbol();
489 } else if (Subtarget->isPICStyleGOT())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 O << "@GOTOFF";
491 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000492
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000493 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494
495 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
496 O << "(%rip)";
497 return;
498 }
499 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000500 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 bool needCloseParen = false;
502
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000503 const GlobalValue *GV = MO.getGlobal();
504 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
505 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000506 // If GV is an alias then use the aliasee for determining
507 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000508 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000509 GVar =dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000510 }
511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 bool isThreadLocal = GVar && GVar->isThreadLocal();
513
514 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000515 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000516
Chris Lattner357a0ca2009-06-20 19:34:09 +0000517 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 O << '$';
519 else if (Name[0] == '$') {
520 // The name begins with a dollar-sign. In order to avoid having it look
521 // like an integer immediate to the assembler, enclose it in parens.
522 O << '(';
523 needCloseParen = true;
524 }
525
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000526 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 // Link-once, declaration, or Weakly-linked global variables need
528 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000529 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000531 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-12-05 01:06:39 +0000532 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
533 // Definition is not definitely in the current translation unit.
534 O << Name;
535 else {
536 HiddenGVStubs.insert(Name);
537 printSuffixedName(Name, "$non_lazy_ptr");
538 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 } else {
540 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000541 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 }
543 } else {
544 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000545 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 O << Name;
547 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000548
Chris Lattner14f791a2009-06-24 19:19:16 +0000549 if (TM.getRelocationModel() == Reloc::PIC_) {
550 O << '-';
551 PrintPICBaseSymbol();
552 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000554 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000555 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 }
558
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000559 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560
Chris Lattnerdabfe572009-06-21 02:22:53 +0000561 if (needCloseParen)
562 O << ')';
563
564 bool isRIPRelative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000566 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
567 switch (model) {
568 case TLSModel::GeneralDynamic:
569 O << "@TLSGD";
570 break;
571 case TLSModel::LocalDynamic:
572 // O << "@TLSLD"; // local dynamic not implemented
Bill Wendlingc1946742009-05-30 01:09:53 +0000573 O << "@TLSGD";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000574 break;
575 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000576 if (Subtarget->is64Bit()) {
577 assert (!NotRIPRel);
Chris Lattnerdabfe572009-06-21 02:22:53 +0000578 O << "@GOTTPOFF";
579 isRIPRelative = true;
Rafael Espindolab93a5122009-04-13 13:02:49 +0000580 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000581 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000582 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000583 break;
584 case TLSModel::LocalExec:
585 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000586 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000587 else
Bill Wendlingc1946742009-05-30 01:09:53 +0000588 O << "@NTPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000589 break;
590 default:
591 assert (0 && "Unknown TLS model");
592 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000594 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
596 O << "@GOT";
597 else
598 O << "@GOTOFF";
Chris Lattnerdabfe572009-06-21 02:22:53 +0000599 } else if (Subtarget->isPICStyleRIPRel() &&
600 !NotRIPRel) {
Dan Gohmane254f322009-03-14 02:33:41 +0000601 if (TM.getRelocationModel() != Reloc::Static) {
602 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
603 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 }
Chris Lattnerdabfe572009-06-21 02:22:53 +0000605
606 isRIPRelative = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 }
608 }
609
Chris Lattnerdabfe572009-06-21 02:22:53 +0000610 // Use rip when possible to reduce code size, except when
611 // index or base register are also part of the address. e.g.
612 // foo(%rip)(%rcx,%rax,4) is not legal.
613 if (isRIPRelative)
614 O << "(%rip)";
615
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 return;
617 }
618 case MachineOperand::MO_ExternalSymbol: {
Dan Gohman96f096e2009-04-23 00:57:37 +0000619 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 bool needCloseParen = false;
621 std::string Name(TAI->getGlobalPrefix());
622 Name += MO.getSymbolName();
Chris Lattner357a0ca2009-06-20 19:34:09 +0000623
Evan Chengdfd884e2008-09-20 00:13:45 +0000624 // Print function stub suffix unless it's Mac OS X 10.5 and up.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000625 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 O << '$';
627 else if (Name[0] == '$') {
628 // The name begins with a dollar-sign. In order to avoid having it look
629 // like an integer immediate to the assembler, enclose it in parens.
630 O << '(';
631 needCloseParen = true;
632 }
633
634 O << Name;
635
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000636 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 std::string GOTName(TAI->getGlobalPrefix());
638 GOTName+="_GLOBAL_OFFSET_TABLE_";
Chris Lattner14f791a2009-06-24 19:19:16 +0000639 if (Name == GOTName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 // HACK! Emit extra offset to PC during printing GOT offset to
641 // compensate for the size of popl instruction. The resulting code
642 // should look like:
643 // call .piclabel
644 // piclabel:
645 // popl %some_register
646 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Chris Lattner14f791a2009-06-24 19:19:16 +0000647 O << " + [.-";
648 PrintPICBaseSymbol();
649 O << ']';
650 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 }
652
653 if (needCloseParen)
654 O << ')';
655
Chris Lattner357a0ca2009-06-20 19:34:09 +0000656 if (Subtarget->isPICStyleRIPRel())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 O << "(%rip)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 return;
659 }
660 default:
661 O << "<unknown operand type>"; return;
662 }
663}
664
665void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000666 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 assert(value <= 7 && "Invalid ssecc argument!");
668 switch (value) {
669 case 0: O << "eq"; break;
670 case 1: O << "lt"; break;
671 case 2: O << "le"; break;
672 case 3: O << "unord"; break;
673 case 4: O << "neq"; break;
674 case 5: O << "nlt"; break;
675 case 6: O << "nle"; break;
676 case 7: O << "ord"; break;
677 }
678}
679
Rafael Espindolabca99f72009-04-08 21:14:34 +0000680void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000681 const char *Modifier,
682 bool NotRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 MachineOperand BaseReg = MI->getOperand(Op);
684 MachineOperand IndexReg = MI->getOperand(Op+2);
685 const MachineOperand &DispSpec = MI->getOperand(Op+3);
686
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000687 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000688 if (DispSpec.isGlobal() ||
689 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000690 DispSpec.isJTI() ||
691 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 printOperand(MI, Op+3, "mem", NotRIPRel);
693 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000694 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
696 O << DispVal;
697 }
698
699 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000700 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000702
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 // There are cases where we can end up with ESP/RSP in the indexreg slot.
704 // If this happens, swap the base/index register to support assemblers that
705 // don't work when the index is *SP.
706 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
707 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
708 std::swap(BaseReg, IndexReg);
709 std::swap(BaseRegOperand, IndexRegOperand);
710 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000711
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000712 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 if (BaseReg.getReg())
714 printOperand(MI, Op+BaseRegOperand, Modifier);
715
716 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000717 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 printOperand(MI, Op+IndexRegOperand, Modifier);
719 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000720 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000722 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 }
724}
725
Rafael Espindolabca99f72009-04-08 21:14:34 +0000726void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000727 const char *Modifier, bool NotRIPRel){
Rafael Espindolabca99f72009-04-08 21:14:34 +0000728 assert(isMem(MI, Op) && "Invalid memory reference!");
729 MachineOperand Segment = MI->getOperand(Op+4);
730 if (Segment.getReg()) {
731 printOperand(MI, Op+4, Modifier);
732 O << ':';
733 }
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000734 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000735}
736
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000737void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000738 const MachineBasicBlock *MBB) const {
739 if (!TAI->getSetDirective())
740 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000741
742 // We don't need .set machinery if we have GOT-style relocations
743 if (Subtarget->isPICStyleGOT())
744 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000745
Evan Cheng6fb06762007-11-09 01:32:10 +0000746 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
747 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000748 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000749 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000750 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000751 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000752 else {
753 O << '-';
754 PrintPICBaseSymbol();
755 O << '\n';
756 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000757}
758
Chris Lattner14f791a2009-06-24 19:19:16 +0000759
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000761 PrintPICBaseSymbol();
762 O << '\n';
763 PrintPICBaseSymbol();
764 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765}
766
767
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000768void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
769 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000770 unsigned uid) const
771{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000772 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
773 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
774
775 O << JTEntryDirective << ' ';
776
777 if (TM.getRelocationModel() == Reloc::PIC_) {
778 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
779 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
780 << '_' << uid << "_set_" << MBB->getNumber();
781 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000782 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000783 O << "@GOTOFF";
784 } else
785 assert(0 && "Don't know how to print MBB label for this PIC mode");
786 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000787 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000788}
789
Chris Lattner8bb96e42009-06-15 04:42:32 +0000790bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 unsigned Reg = MO.getReg();
792 switch (Mode) {
793 default: return true; // Unknown mode.
794 case 'b': // Print QImode register
795 Reg = getX86SubSuperRegister(Reg, MVT::i8);
796 break;
797 case 'h': // Print QImode high register
798 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
799 break;
800 case 'w': // Print HImode register
801 Reg = getX86SubSuperRegister(Reg, MVT::i16);
802 break;
803 case 'k': // Print SImode register
804 Reg = getX86SubSuperRegister(Reg, MVT::i32);
805 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000806 case 'q': // Print DImode register
807 Reg = getX86SubSuperRegister(Reg, MVT::i64);
808 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 }
810
Evan Cheng00d04a72008-07-07 22:21:06 +0000811 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 return false;
813}
814
815/// PrintAsmOperand - Print out an operand for an inline asm expression.
816///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000817bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000818 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 const char *ExtraCode) {
820 // Does this asm operand have a single letter operand modifier?
821 if (ExtraCode && ExtraCode[0]) {
822 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000823
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 switch (ExtraCode[0]) {
825 default: return true; // Unknown modifier.
826 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000827 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 return false;
829 case 'b': // Print QImode register
830 case 'h': // Print QImode high register
831 case 'w': // Print HImode register
832 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000833 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000834 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
836 printOperand(MI, OpNo);
837 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000840 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 return false;
842 }
843 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000844
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 printOperand(MI, OpNo);
846 return false;
847}
848
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000849bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000851 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000853 if (ExtraCode && ExtraCode[0]) {
854 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000855
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000856 switch (ExtraCode[0]) {
857 default: return true; // Unknown modifier.
858 case 'b': // Print QImode register
859 case 'h': // Print QImode high register
860 case 'w': // Print HImode register
861 case 'k': // Print SImode register
862 case 'q': // Print SImode register
863 // These only apply to registers, ignore on mem.
864 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000865 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000866 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattnerd1595722009-01-23 22:33:40 +0000867 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000868 }
869 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 printMemReference(MI, OpNo);
871 return false;
872}
873
Chris Lattnerf5da5902009-06-20 07:03:18 +0000874static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
875 // Convert registers in the addr mode according to subreg64.
876 for (unsigned i = 0; i != 4; ++i) {
877 if (!MI->getOperand(i).isReg()) continue;
878
879 unsigned Reg = MI->getOperand(i).getReg();
880 if (Reg == 0) continue;
881
882 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
883 }
884}
885
Bill Wendlingb3b11262009-02-06 21:45:08 +0000886/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
887/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888///
889void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
890 ++EmittedInsts;
891
Chris Lattner19b1bd52009-06-19 00:47:33 +0000892 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000893 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
894 O << "\t";
895 printInlineAsm(MI);
896 return;
897 } else if (MI->isLabel()) {
898 printLabel(MI);
899 return;
900 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
901 printDeclare(MI);
902 return;
903 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
904 printImplicitDef(MI);
905 return;
906 }
907
Chris Lattnere67184e2009-06-19 23:59:57 +0000908 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000909 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000910
911 TmpInst.setOpcode(MI->getOpcode());
912
913 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
914 const MachineOperand &MO = MI->getOperand(i);
915
916 MCOperand MCOp;
917 if (MO.isReg()) {
918 MCOp.MakeReg(MO.getReg());
919 } else if (MO.isImm()) {
920 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000921 } else if (MO.isMBB()) {
922 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000923 } else {
924 assert(0 && "Unimp");
925 }
926
927 TmpInst.addOperand(MCOp);
928 }
929
Chris Lattner6b9f7742009-06-20 07:59:10 +0000930 switch (TmpInst.getOpcode()) {
931 case X86::LEA64_32r:
932 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000933 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000934 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000935 }
936
Chris Lattner19b1bd52009-06-19 00:47:33 +0000937 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000938 printInstruction(&TmpInst);
939 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000940 }
941
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 // Call the autogenerated instruction printer routines.
943 printInstruction(MI);
944}
945
Devang Patelc20079e2009-06-20 01:00:07 +0000946/// doInitialization
947bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000948 if (NewAsmPrinter) {
949 Context = new MCContext();
950 // FIXME: Send this to "O" instead of outs(). For now, we force it to
951 // stdout to make it easy to compare.
952 Streamer = createAsmStreamer(*Context, outs());
953 }
954
Devang Patelc20079e2009-06-20 01:00:07 +0000955 return AsmPrinter::doInitialization(M);
956}
957
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000958void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000959 const TargetData *TD = TM.getTargetData();
960
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000961 if (!GVar->hasInitializer())
962 return; // External global require no code
963
964 // Check to see if this is a special global used by LLVM, if so, emit it.
965 if (EmitSpecialLLVMGlobal(GVar)) {
966 if (Subtarget->isTargetDarwin() &&
967 TM.getRelocationModel() == Reloc::Static) {
968 if (GVar->getName() == "llvm.global_ctors")
969 O << ".reference .constructors_used\n";
970 else if (GVar->getName() == "llvm.global_dtors")
971 O << ".reference .destructors_used\n";
972 }
973 return;
974 }
975
976 std::string name = Mang->getValueName(GVar);
977 Constant *C = GVar->getInitializer();
Devang Patelf667ab42009-06-25 00:47:42 +0000978 if (isa<MDNode>(C))
979 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000980 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000981 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000982 unsigned Align = TD->getPreferredAlignmentLog(GVar);
983
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000984 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000985
986 if (Subtarget->isTargetELF())
987 O << "\t.type\t" << name << ",@object\n";
988
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000989 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000990
Evan Chengcf84b142009-02-18 02:19:52 +0000991 if (C->isNullValue() && !GVar->hasSection() &&
992 !(Subtarget->isTargetDarwin() &&
993 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000994 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000995 if (GVar->hasExternalLinkage()) {
996 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000997 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000998 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000999 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001000 return;
1001 }
1002 }
1003
1004 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +00001005 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001006 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +00001007
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001008 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001009 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001010 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001011 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001012 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001013 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001014 O << "\t.globl " << name << '\n'
1015 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001016 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001017 O << name << ":";
1018 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +00001019 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001020 PrintUnmangledNameSafely(GVar, O);
1021 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001022 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001023 EmitGlobalConstant(C);
1024 return;
1025 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001026 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +00001027 if (TAI->getCOMMDirectiveTakesAlignment())
1028 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001029 }
1030 } else {
1031 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001032 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001033 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001034 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001035 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001036 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001037 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001038 }
Evan Cheng11db8142009-03-24 00:17:40 +00001039 if (VerboseAsm) {
1040 O << "\t\t" << TAI->getCommentString() << ' ';
1041 PrintUnmangledNameSafely(GVar, O);
1042 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001043 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001044 return;
1045 }
1046 }
1047
1048 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001049 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001050 case GlobalValue::LinkOnceAnyLinkage:
1051 case GlobalValue::LinkOnceODRLinkage:
1052 case GlobalValue::WeakAnyLinkage:
1053 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001054 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001055 O << "\t.globl " << name << '\n'
1056 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001057 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001058 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001059 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001060 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001061 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001062 }
1063 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001064 case GlobalValue::DLLExportLinkage:
1065 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001066 // FIXME: appending linkage variables should go into a section of
1067 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +00001068 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001069 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001070 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001071 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001072 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001073 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001074 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001075 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001076 assert(0 && "Unknown linkage type!");
1077 }
1078
1079 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001080 O << name << ":";
1081 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +00001082 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001083 PrintUnmangledNameSafely(GVar, O);
1084 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001085 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001086 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001087 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001088
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001089 EmitGlobalConstant(C);
1090}
1091
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001092bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001093 // Print out module-level global variables here.
1094 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001095 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001096 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001097
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001098 if (I->hasDLLExportLinkage())
1099 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001100 }
1101
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001102 if (Subtarget->isTargetDarwin()) {
1103 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +00001104
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001105 // Add the (possibly multiple) personalities to the set of global value
1106 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001107 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001108 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1109 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
1110 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +00001111 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001112 std::string Name = Mang->getValueName(Personalities[i]);
1113 decorateName(Name, Personalities[i]);
1114 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +00001115 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001116 }
1117
Chris Lattner2e860262009-06-24 18:24:09 +00001118 // Output stubs for dynamically-linked functions
1119 if (!FnStubs.empty()) {
1120 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
1121 I != E; ++I) {
1122 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1123 "self_modifying_code+pure_instructions,5", 0);
1124 const char *Name = I->getKeyData();
1125 printSuffixedName(Name, "$stub");
1126 O << ":\n"
1127 "\t.indirect_symbol " << Name << "\n"
1128 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1129 }
1130 O << '\n';
1131 }
1132
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001133 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001134 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001135 SwitchToDataSection(
1136 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001137 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1138 I != E; ++I) {
1139 const char *Name = I->getKeyData();
1140 printSuffixedName(Name, "$non_lazy_ptr");
1141 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1142 }
1143 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001144
Evan Chenga65854f2008-12-05 01:06:39 +00001145 if (!HiddenGVStubs.empty()) {
1146 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001147 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001148 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1149 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001150 const char *Name = I->getKeyData();
1151 printSuffixedName(Name, "$non_lazy_ptr");
1152 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1153 }
Evan Chenga65854f2008-12-05 01:06:39 +00001154 }
1155
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001156 // Funny Darwin hack: This flag tells the linker that no global symbols
1157 // contain code that falls through to other global symbols (e.g. the obvious
1158 // implementation of multiple entry points). If this doesn't occur, the
1159 // linker can safely perform dead code stripping. Since LLVM never
1160 // generates code that does this, it is always safe to set.
1161 O << "\t.subsections_via_symbols\n";
1162 } else if (Subtarget->isTargetCygMing()) {
1163 // Emit type information for external functions
1164 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1165 i != e; ++i) {
1166 O << "\t.def\t " << i->getKeyData()
1167 << ";\t.scl\t" << COFF::C_EXT
1168 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1169 << ";\t.endef\n";
1170 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001171 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001172
Chris Lattnerdb191f02009-06-24 18:52:01 +00001173
1174 // Output linker support code for dllexported globals on windows.
1175 if (!DLLExportedGVs.empty()) {
1176 SwitchToDataSection(".section .drectve");
1177
1178 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1179 e = DLLExportedGVs.end(); i != e; ++i)
1180 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1181 }
1182
1183 if (!DLLExportedFns.empty()) {
1184 SwitchToDataSection(".section .drectve");
1185
1186 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1187 e = DLLExportedFns.end();
1188 i != e; ++i)
1189 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1190 }
1191
Chris Lattnerdb191f02009-06-24 18:52:01 +00001192 // Do common shutdown.
1193 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001194
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001195 if (NewAsmPrinter) {
1196 Streamer->Finish();
1197
1198 delete Streamer;
1199 delete Context;
1200 Streamer = 0;
1201 Context = 0;
1202 }
1203
Chris Lattnerdb191f02009-06-24 18:52:01 +00001204 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001205}
1206
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001207// Include the auto-generated portion of the assembly writer.
1208#include "X86GenAsmWriter.inc"