blob: 0b55a12adf6d653acb1c25b6b0d1f171c0c500a9 [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
Chris Lattner2e845952009-06-24 19:44:36 +000046//===----------------------------------------------------------------------===//
47// Primitive Helper Functions.
48//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000049
50void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000052 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 else if (Subtarget->isTargetELF())
Chris Lattner14f791a2009-06-24 19:19:16 +000054 O << ".Lllvm$" << getFunctionNumber() << "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 else
56 assert(0 && "Don't know how to print PIC label!\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057}
58
Chris Lattner2e845952009-06-24 19:44:36 +000059/// PrintUnmangledNameSafely - Print out the printable characters in the name.
60/// Don't print things like \\n or \\0.
61static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
62 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
63 Name != E; ++Name)
64 if (isprint(*Name))
65 OS << *Name;
66}
Chris Lattner14f791a2009-06-24 19:19:16 +000067
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000068static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
69 const TargetData *TD) {
70 X86MachineFunctionInfo Info;
71 uint64_t Size = 0;
72
73 switch (F->getCallingConv()) {
74 case CallingConv::X86_StdCall:
75 Info.setDecorationStyle(StdCall);
76 break;
77 case CallingConv::X86_FastCall:
78 Info.setDecorationStyle(FastCall);
79 break;
80 default:
81 return Info;
82 }
83
84 unsigned argNum = 1;
85 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
86 AI != AE; ++AI, ++argNum) {
87 const Type* Ty = AI->getType();
88
89 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000090 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000091 Ty = cast<PointerType>(Ty)->getElementType();
92
93 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000094 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000095 }
96
97 // We're not supporting tooooo huge arguments :)
98 Info.setBytesToPopOnReturn((unsigned int)Size);
99 return Info;
100}
101
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000102/// decorateName - Query FunctionInfoMap and use this information for various
103/// name decoration.
104void X86ATTAsmPrinter::decorateName(std::string &Name,
105 const GlobalValue *GV) {
106 const Function *F = dyn_cast<Function>(GV);
107 if (!F) return;
108
109 // We don't want to decorate non-stdcall or non-fastcall functions right now
110 unsigned CC = F->getCallingConv();
111 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
112 return;
113
114 // Decorate names only when we're targeting Cygwin/Mingw32 targets
115 if (!Subtarget->isTargetCygMing())
116 return;
117
118 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
119
120 const X86MachineFunctionInfo *Info;
121 if (info_item == FunctionInfoMap.end()) {
122 // Calculate apropriate function info and populate map
123 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
124 Info = &FunctionInfoMap[F];
125 } else {
126 Info = &info_item->second;
127 }
128
129 const FunctionType *FT = F->getFunctionType();
130 switch (Info->getDecorationStyle()) {
131 case None:
132 break;
133 case StdCall:
134 // "Pure" variadic functions do not receive @0 suffix.
135 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
137 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
138 break;
139 case FastCall:
140 // "Pure" variadic functions do not receive @0 suffix.
141 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
142 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
143 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
144
145 if (Name[0] == '_') {
146 Name[0] = '@';
147 } else {
148 Name = '@' + Name;
149 }
150 break;
151 default:
152 assert(0 && "Unsupported DecorationStyle");
153 }
154}
155
Chris Lattner2e845952009-06-24 19:44:36 +0000156
157
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000158void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000161 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000163 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000164
Chris Lattner2e845952009-06-24 19:44:36 +0000165 // FIXME: A function's alignment should be part of MachineFunction. There
166 // shouldn't be a policy decision here.
Devang Patel93698d92008-10-01 23:18:38 +0000167 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000168 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000169 FnAlign = 1;
Chris Lattner2e845952009-06-24 19:44:36 +0000170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 switch (F->getLinkage()) {
172 default: assert(0 && "Unknown linkage type!");
173 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000174 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 break;
177 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000179 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000182 case Function::LinkOnceAnyLinkage:
183 case Function::LinkOnceODRLinkage:
184 case Function::WeakAnyLinkage:
185 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000186 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << '\n';
189 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000191 O << "\t.globl\t" << CurrentFnName << "\n"
192 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000194 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196 break;
197 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000198
199 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
201 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000202 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 else if (Subtarget->isTargetCygMing()) {
204 O << "\t.def\t " << CurrentFnName
205 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000206 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
208 << ";\t.endef\n";
209 }
210
211 O << CurrentFnName << ":\n";
212 // Add some workaround for linkonce linkage on Cygwin\MinGW
213 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000214 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000216}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000218/// runOnMachineFunction - This uses the printMachineInstruction()
219/// method to print assembly for each instruction.
220///
221bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
222 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000223 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000224 unsigned CC = F->getCallingConv();
225
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000226 SetupMachineFunction(MF);
227 O << "\n\n";
228
229 // Populate function information map. Actually, We don't want to populate
230 // non-stdcall or non-fastcall functions' information right now.
231 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
232 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
233
234 // Print out constants referenced by the function
235 EmitConstantPool(MF.getConstantPool());
236
237 if (F->hasDLLExportLinkage())
238 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
239
240 // Print the 'header' of function
241 emitFunctionHeader(MF);
242
243 // Emit pre-function debug and/or EH information.
244 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000245 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000248 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
250 I != E; ++I) {
251 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000252 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
253 // This is an entry block or a block that's only reachable via a
254 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
255 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000256 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 O << '\n';
258 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000259 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
260 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000262 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000263 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 printMachineInstruction(II);
265 }
266 }
267
Dale Johannesenf35771f2008-04-08 00:37:56 +0000268 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
269 // If the function is empty, then we need to emit *something*. Otherwise,
270 // the function's label might be associated with something that it wasn't
271 // meant to be associated with. We emit a noop in this situation.
272 // We are assuming inline asms are code.
273 O << "\tnop\n";
274 }
275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000277 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000279 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000280 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000281 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
283 // Print out jump tables referenced by the function.
284 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000285
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000286 O.flush();
287
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 // We didn't modify anything.
289 return false;
290}
291
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000292static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
294}
295
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000296static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
297 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
298 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
299}
300
301static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
303}
304
Chris Lattner357a0ca2009-06-20 19:34:09 +0000305/// print_pcrel_imm - This is used to print an immediate value that ends up
306/// being encoded as a pc-relative value. These print slightly differently, for
307/// example, a $ is not emitted.
308void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
309 const MachineOperand &MO = MI->getOperand(OpNo);
310 switch (MO.getType()) {
311 default: assert(0 && "Unknown pcrel immediate operand");
312 case MachineOperand::MO_Immediate:
313 O << MO.getImm();
314 return;
315 case MachineOperand::MO_MachineBasicBlock:
316 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
317 return;
318
319 case MachineOperand::MO_GlobalAddress: {
320 const GlobalValue *GV = MO.getGlobal();
321 std::string Name = Mang->getValueName(GV);
322 decorateName(Name, GV);
323
324 bool needCloseParen = false;
325 if (Name[0] == '$') {
326 // The name begins with a dollar-sign. In order to avoid having it look
327 // like an integer immediate to the assembler, enclose it in parens.
328 O << '(';
329 needCloseParen = true;
330 }
331
332 if (shouldPrintStub(TM, Subtarget)) {
333 // Link-once, declaration, or Weakly-linked global variables need
334 // non-lazily-resolved stubs
335 if (GV->isDeclaration() || GV->isWeakForLinker()) {
336 // Dynamically-resolved functions need a stub for the function.
337 if (isa<Function>(GV)) {
338 // Function stubs are no longer needed for Mac OS X 10.5 and up.
339 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
340 O << Name;
341 } else {
342 FnStubs.insert(Name);
343 printSuffixedName(Name, "$stub");
344 }
345 } else if (GV->hasHiddenVisibility()) {
346 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
347 // Definition is not definitely in the current translation unit.
348 O << Name;
349 else {
350 HiddenGVStubs.insert(Name);
351 printSuffixedName(Name, "$non_lazy_ptr");
352 }
353 } else {
354 GVStubs.insert(Name);
355 printSuffixedName(Name, "$non_lazy_ptr");
356 }
357 } else {
358 if (GV->hasDLLImportLinkage())
359 O << "__imp_";
360 O << Name;
361 }
362 } else {
363 if (GV->hasDLLImportLinkage()) {
364 O << "__imp_";
365 }
366 O << Name;
367
368 if (shouldPrintPLT(TM, Subtarget)) {
369 // Assemble call via PLT for externally visible symbols
370 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
371 !GV->hasLocalLinkage())
372 O << "@PLT";
373 }
374 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
375 // Save function name for later type emission
376 FnStubs.insert(Name);
377 }
378
Chris Lattner357a0ca2009-06-20 19:34:09 +0000379 printOffset(MO.getOffset());
380
381 if (needCloseParen)
382 O << ')';
383 return;
384 }
385
386 case MachineOperand::MO_ExternalSymbol: {
387 bool needCloseParen = false;
388 std::string Name(TAI->getGlobalPrefix());
389 Name += MO.getSymbolName();
390 // Print function stub suffix unless it's Mac OS X 10.5 and up.
391 if (shouldPrintStub(TM, Subtarget) &&
392 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
393 FnStubs.insert(Name);
394 printSuffixedName(Name, "$stub");
395 return;
396 }
397
398 if (Name[0] == '$') {
399 // The name begins with a dollar-sign. In order to avoid having it look
400 // like an integer immediate to the assembler, enclose it in parens.
401 O << '(';
402 needCloseParen = true;
403 }
404
405 O << Name;
406
407 if (shouldPrintPLT(TM, Subtarget)) {
408 std::string GOTName(TAI->getGlobalPrefix());
409 GOTName+="_GLOBAL_OFFSET_TABLE_";
Chris Lattner14f791a2009-06-24 19:19:16 +0000410 if (Name == GOTName) {
Chris Lattner357a0ca2009-06-20 19:34:09 +0000411 // HACK! Emit extra offset to PC during printing GOT offset to
412 // compensate for the size of popl instruction. The resulting code
413 // should look like:
414 // call .piclabel
415 // piclabel:
416 // popl %some_register
417 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Chris Lattner14f791a2009-06-24 19:19:16 +0000418 O << " + [.-";
419 PrintPICBaseSymbol();
420 O << ']';
421 }
Chris Lattner357a0ca2009-06-20 19:34:09 +0000422
423 O << "@PLT";
424 }
425
426 if (needCloseParen)
427 O << ')';
428
429 return;
430 }
431 }
432}
433
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
435 const char *Modifier, bool NotRIPRel) {
436 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 switch (MO.getType()) {
438 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000439 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 "Virtual registers should not make it this far!");
441 O << '%';
442 unsigned Reg = MO.getReg();
443 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000444 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
446 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
447 Reg = getX86SubSuperRegister(Reg, VT);
448 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000449 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 return;
451 }
452
453 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000454 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000455 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000457 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 case MachineOperand::MO_JumpTableIndex: {
460 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
461 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000462 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000463 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464
465 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2e845952009-06-24 19:44:36 +0000466 if (Subtarget->isPICStyleStub()) {
467 O << '-';
468 PrintPICBaseSymbol();
469 } else if (Subtarget->isPICStyleGOT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 O << "@GOTOFF";
Chris Lattner2e845952009-06-24 19:44:36 +0000471 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000473
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
475 O << "(%rip)";
476 return;
477 }
478 case MachineOperand::MO_ConstantPoolIndex: {
479 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
480 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000481 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000482 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483
484 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2e845952009-06-24 19:44:36 +0000485 if (Subtarget->isPICStyleStub()) {
486 O << '-';
487 PrintPICBaseSymbol();
488 } else if (Subtarget->isPICStyleGOT())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 O << "@GOTOFF";
490 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000491
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000492 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493
494 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
495 O << "(%rip)";
496 return;
497 }
498 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000499 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 bool needCloseParen = false;
501
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000502 const GlobalValue *GV = MO.getGlobal();
503 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
504 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000505 // If GV is an alias then use the aliasee for determining
506 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000507 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000508 GVar =dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000509 }
510
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 bool isThreadLocal = GVar && GVar->isThreadLocal();
512
513 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000514 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000515
Chris Lattner357a0ca2009-06-20 19:34:09 +0000516 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 O << '$';
518 else if (Name[0] == '$') {
519 // The name begins with a dollar-sign. In order to avoid having it look
520 // like an integer immediate to the assembler, enclose it in parens.
521 O << '(';
522 needCloseParen = true;
523 }
524
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000525 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 // Link-once, declaration, or Weakly-linked global variables need
527 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000528 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000530 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-12-05 01:06:39 +0000531 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
532 // Definition is not definitely in the current translation unit.
533 O << Name;
534 else {
535 HiddenGVStubs.insert(Name);
536 printSuffixedName(Name, "$non_lazy_ptr");
537 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 } else {
539 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000540 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 }
542 } else {
543 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000544 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 O << Name;
546 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000547
Chris Lattner14f791a2009-06-24 19:19:16 +0000548 if (TM.getRelocationModel() == Reloc::PIC_) {
549 O << '-';
550 PrintPICBaseSymbol();
551 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000553 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000554 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 }
557
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000558 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559
Chris Lattnerdabfe572009-06-21 02:22:53 +0000560 if (needCloseParen)
561 O << ')';
562
563 bool isRIPRelative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000565 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
566 switch (model) {
567 case TLSModel::GeneralDynamic:
568 O << "@TLSGD";
569 break;
570 case TLSModel::LocalDynamic:
571 // O << "@TLSLD"; // local dynamic not implemented
Bill Wendlingc1946742009-05-30 01:09:53 +0000572 O << "@TLSGD";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000573 break;
574 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000575 if (Subtarget->is64Bit()) {
576 assert (!NotRIPRel);
Chris Lattnerdabfe572009-06-21 02:22:53 +0000577 O << "@GOTTPOFF";
578 isRIPRelative = true;
Rafael Espindolab93a5122009-04-13 13:02:49 +0000579 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000580 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000581 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000582 break;
583 case TLSModel::LocalExec:
584 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000585 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000586 else
Bill Wendlingc1946742009-05-30 01:09:53 +0000587 O << "@NTPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000588 break;
589 default:
590 assert (0 && "Unknown TLS model");
591 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000593 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
595 O << "@GOT";
596 else
597 O << "@GOTOFF";
Chris Lattnerdabfe572009-06-21 02:22:53 +0000598 } else if (Subtarget->isPICStyleRIPRel() &&
599 !NotRIPRel) {
Dan Gohmane254f322009-03-14 02:33:41 +0000600 if (TM.getRelocationModel() != Reloc::Static) {
601 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
602 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 }
Chris Lattnerdabfe572009-06-21 02:22:53 +0000604
605 isRIPRelative = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 }
607 }
608
Chris Lattnerdabfe572009-06-21 02:22:53 +0000609 // Use rip when possible to reduce code size, except when
610 // index or base register are also part of the address. e.g.
611 // foo(%rip)(%rcx,%rax,4) is not legal.
612 if (isRIPRelative)
613 O << "(%rip)";
614
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 return;
616 }
617 case MachineOperand::MO_ExternalSymbol: {
Dan Gohman96f096e2009-04-23 00:57:37 +0000618 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 bool needCloseParen = false;
620 std::string Name(TAI->getGlobalPrefix());
621 Name += MO.getSymbolName();
Chris Lattner357a0ca2009-06-20 19:34:09 +0000622
Evan Chengdfd884e2008-09-20 00:13:45 +0000623 // Print function stub suffix unless it's Mac OS X 10.5 and up.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000624 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 O << '$';
626 else if (Name[0] == '$') {
627 // The name begins with a dollar-sign. In order to avoid having it look
628 // like an integer immediate to the assembler, enclose it in parens.
629 O << '(';
630 needCloseParen = true;
631 }
632
633 O << Name;
634
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000635 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 std::string GOTName(TAI->getGlobalPrefix());
637 GOTName+="_GLOBAL_OFFSET_TABLE_";
Chris Lattner14f791a2009-06-24 19:19:16 +0000638 if (Name == GOTName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 // HACK! Emit extra offset to PC during printing GOT offset to
640 // compensate for the size of popl instruction. The resulting code
641 // should look like:
642 // call .piclabel
643 // piclabel:
644 // popl %some_register
645 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Chris Lattner14f791a2009-06-24 19:19:16 +0000646 O << " + [.-";
647 PrintPICBaseSymbol();
648 O << ']';
649 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 }
651
652 if (needCloseParen)
653 O << ')';
654
Chris Lattner357a0ca2009-06-20 19:34:09 +0000655 if (Subtarget->isPICStyleRIPRel())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 O << "(%rip)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 return;
658 }
659 default:
660 O << "<unknown operand type>"; return;
661 }
662}
663
664void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000665 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 assert(value <= 7 && "Invalid ssecc argument!");
667 switch (value) {
668 case 0: O << "eq"; break;
669 case 1: O << "lt"; break;
670 case 2: O << "le"; break;
671 case 3: O << "unord"; break;
672 case 4: O << "neq"; break;
673 case 5: O << "nlt"; break;
674 case 6: O << "nle"; break;
675 case 7: O << "ord"; break;
676 }
677}
678
Rafael Espindolabca99f72009-04-08 21:14:34 +0000679void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000680 const char *Modifier,
681 bool NotRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 MachineOperand BaseReg = MI->getOperand(Op);
683 MachineOperand IndexReg = MI->getOperand(Op+2);
684 const MachineOperand &DispSpec = MI->getOperand(Op+3);
685
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000686 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000687 if (DispSpec.isGlobal() ||
688 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000689 DispSpec.isJTI() ||
690 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 printOperand(MI, Op+3, "mem", NotRIPRel);
692 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000693 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
695 O << DispVal;
696 }
697
698 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000699 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000701
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 // There are cases where we can end up with ESP/RSP in the indexreg slot.
703 // If this happens, swap the base/index register to support assemblers that
704 // don't work when the index is *SP.
705 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
706 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
707 std::swap(BaseReg, IndexReg);
708 std::swap(BaseRegOperand, IndexRegOperand);
709 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000710
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000711 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 if (BaseReg.getReg())
713 printOperand(MI, Op+BaseRegOperand, Modifier);
714
715 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000716 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 printOperand(MI, Op+IndexRegOperand, Modifier);
718 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000719 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000721 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 }
723}
724
Rafael Espindolabca99f72009-04-08 21:14:34 +0000725void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000726 const char *Modifier, bool NotRIPRel){
Rafael Espindolabca99f72009-04-08 21:14:34 +0000727 assert(isMem(MI, Op) && "Invalid memory reference!");
728 MachineOperand Segment = MI->getOperand(Op+4);
729 if (Segment.getReg()) {
730 printOperand(MI, Op+4, Modifier);
731 O << ':';
732 }
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000733 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000734}
735
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000736void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000737 const MachineBasicBlock *MBB) const {
738 if (!TAI->getSetDirective())
739 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000740
741 // We don't need .set machinery if we have GOT-style relocations
742 if (Subtarget->isPICStyleGOT())
743 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000744
Evan Cheng6fb06762007-11-09 01:32:10 +0000745 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
746 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000747 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000748 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000749 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000750 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000751 else {
752 O << '-';
753 PrintPICBaseSymbol();
754 O << '\n';
755 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000756}
757
Chris Lattner14f791a2009-06-24 19:19:16 +0000758
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000760 PrintPICBaseSymbol();
761 O << '\n';
762 PrintPICBaseSymbol();
763 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764}
765
766
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000767void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
768 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000769 unsigned uid) const
770{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000771 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
772 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
773
774 O << JTEntryDirective << ' ';
775
776 if (TM.getRelocationModel() == Reloc::PIC_) {
777 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
778 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
779 << '_' << uid << "_set_" << MBB->getNumber();
780 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000781 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000782 O << "@GOTOFF";
783 } else
784 assert(0 && "Don't know how to print MBB label for this PIC mode");
785 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000786 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000787}
788
Chris Lattner8bb96e42009-06-15 04:42:32 +0000789bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790 unsigned Reg = MO.getReg();
791 switch (Mode) {
792 default: return true; // Unknown mode.
793 case 'b': // Print QImode register
794 Reg = getX86SubSuperRegister(Reg, MVT::i8);
795 break;
796 case 'h': // Print QImode high register
797 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
798 break;
799 case 'w': // Print HImode register
800 Reg = getX86SubSuperRegister(Reg, MVT::i16);
801 break;
802 case 'k': // Print SImode register
803 Reg = getX86SubSuperRegister(Reg, MVT::i32);
804 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000805 case 'q': // Print DImode register
806 Reg = getX86SubSuperRegister(Reg, MVT::i64);
807 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 }
809
Evan Cheng00d04a72008-07-07 22:21:06 +0000810 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 return false;
812}
813
814/// PrintAsmOperand - Print out an operand for an inline asm expression.
815///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000816bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000817 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 const char *ExtraCode) {
819 // Does this asm operand have a single letter operand modifier?
820 if (ExtraCode && ExtraCode[0]) {
821 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000822
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 switch (ExtraCode[0]) {
824 default: return true; // Unknown modifier.
825 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000826 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 return false;
828 case 'b': // Print QImode register
829 case 'h': // Print QImode high register
830 case 'w': // Print HImode register
831 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000832 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000833 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
835 printOperand(MI, OpNo);
836 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000837
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000839 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 return false;
841 }
842 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000843
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 printOperand(MI, OpNo);
845 return false;
846}
847
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000848bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000850 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000852 if (ExtraCode && ExtraCode[0]) {
853 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000854
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000855 switch (ExtraCode[0]) {
856 default: return true; // Unknown modifier.
857 case 'b': // Print QImode register
858 case 'h': // Print QImode high register
859 case 'w': // Print HImode register
860 case 'k': // Print SImode register
861 case 'q': // Print SImode register
862 // These only apply to registers, ignore on mem.
863 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000864 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000865 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattnerd1595722009-01-23 22:33:40 +0000866 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000867 }
868 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 printMemReference(MI, OpNo);
870 return false;
871}
872
Chris Lattnerf5da5902009-06-20 07:03:18 +0000873static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
874 // Convert registers in the addr mode according to subreg64.
875 for (unsigned i = 0; i != 4; ++i) {
876 if (!MI->getOperand(i).isReg()) continue;
877
878 unsigned Reg = MI->getOperand(i).getReg();
879 if (Reg == 0) continue;
880
881 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
882 }
883}
884
Bill Wendlingb3b11262009-02-06 21:45:08 +0000885/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
886/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887///
888void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
889 ++EmittedInsts;
890
Chris Lattner19b1bd52009-06-19 00:47:33 +0000891 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000892 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
893 O << "\t";
894 printInlineAsm(MI);
895 return;
896 } else if (MI->isLabel()) {
897 printLabel(MI);
898 return;
899 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
900 printDeclare(MI);
901 return;
902 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
903 printImplicitDef(MI);
904 return;
905 }
906
Chris Lattnere67184e2009-06-19 23:59:57 +0000907 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000908 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000909
910 TmpInst.setOpcode(MI->getOpcode());
911
912 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
913 const MachineOperand &MO = MI->getOperand(i);
914
915 MCOperand MCOp;
916 if (MO.isReg()) {
917 MCOp.MakeReg(MO.getReg());
918 } else if (MO.isImm()) {
919 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000920 } else if (MO.isMBB()) {
921 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000922 } else {
923 assert(0 && "Unimp");
924 }
925
926 TmpInst.addOperand(MCOp);
927 }
928
Chris Lattner6b9f7742009-06-20 07:59:10 +0000929 switch (TmpInst.getOpcode()) {
930 case X86::LEA64_32r:
931 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000932 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000933 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000934 }
935
Chris Lattner19b1bd52009-06-19 00:47:33 +0000936 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000937 printInstruction(&TmpInst);
938 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000939 }
940
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 // Call the autogenerated instruction printer routines.
942 printInstruction(MI);
943}
944
Devang Patelc20079e2009-06-20 01:00:07 +0000945/// doInitialization
946bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000947 if (NewAsmPrinter) {
948 Context = new MCContext();
949 // FIXME: Send this to "O" instead of outs(). For now, we force it to
950 // stdout to make it easy to compare.
951 Streamer = createAsmStreamer(*Context, outs());
952 }
953
Devang Patelc20079e2009-06-20 01:00:07 +0000954 return AsmPrinter::doInitialization(M);
955}
956
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000957void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000958 const TargetData *TD = TM.getTargetData();
959
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000960 if (!GVar->hasInitializer())
961 return; // External global require no code
962
963 // Check to see if this is a special global used by LLVM, if so, emit it.
964 if (EmitSpecialLLVMGlobal(GVar)) {
965 if (Subtarget->isTargetDarwin() &&
966 TM.getRelocationModel() == Reloc::Static) {
967 if (GVar->getName() == "llvm.global_ctors")
968 O << ".reference .constructors_used\n";
969 else if (GVar->getName() == "llvm.global_dtors")
970 O << ".reference .destructors_used\n";
971 }
972 return;
973 }
974
975 std::string name = Mang->getValueName(GVar);
976 Constant *C = GVar->getInitializer();
977 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000978 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000979 unsigned Align = TD->getPreferredAlignmentLog(GVar);
980
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000981 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000982
983 if (Subtarget->isTargetELF())
984 O << "\t.type\t" << name << ",@object\n";
985
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000986 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000987
Evan Chengcf84b142009-02-18 02:19:52 +0000988 if (C->isNullValue() && !GVar->hasSection() &&
989 !(Subtarget->isTargetDarwin() &&
990 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000991 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000992 if (GVar->hasExternalLinkage()) {
993 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000994 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000995 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000996 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000997 return;
998 }
999 }
1000
1001 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +00001002 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001003 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +00001004
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001005 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001006 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001007 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001008 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001009 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001010 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001011 O << "\t.globl " << name << '\n'
1012 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001013 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001014 O << name << ":";
1015 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +00001016 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001017 PrintUnmangledNameSafely(GVar, O);
1018 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001019 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001020 EmitGlobalConstant(C);
1021 return;
1022 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001023 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +00001024 if (TAI->getCOMMDirectiveTakesAlignment())
1025 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001026 }
1027 } else {
1028 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001029 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001030 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001031 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001032 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001033 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001034 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001035 }
Evan Cheng11db8142009-03-24 00:17:40 +00001036 if (VerboseAsm) {
1037 O << "\t\t" << TAI->getCommentString() << ' ';
1038 PrintUnmangledNameSafely(GVar, O);
1039 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001040 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001041 return;
1042 }
1043 }
1044
1045 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001046 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001047 case GlobalValue::LinkOnceAnyLinkage:
1048 case GlobalValue::LinkOnceODRLinkage:
1049 case GlobalValue::WeakAnyLinkage:
1050 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001051 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001052 O << "\t.globl " << name << '\n'
1053 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001054 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001055 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001056 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001057 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001058 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001059 }
1060 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001061 case GlobalValue::DLLExportLinkage:
1062 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001063 // FIXME: appending linkage variables should go into a section of
1064 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +00001065 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001066 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001067 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001068 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001069 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001070 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001071 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001072 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001073 assert(0 && "Unknown linkage type!");
1074 }
1075
1076 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001077 O << name << ":";
1078 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +00001079 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001080 PrintUnmangledNameSafely(GVar, O);
1081 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001082 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001083 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001084 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001085
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001086 EmitGlobalConstant(C);
1087}
1088
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001089bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001090 // Print out module-level global variables here.
1091 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001092 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001093 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001094
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001095 if (I->hasDLLExportLinkage())
1096 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001097 }
1098
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001099 if (Subtarget->isTargetDarwin()) {
1100 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +00001101
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001102 // Add the (possibly multiple) personalities to the set of global value
1103 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001104 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001105 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1106 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
1107 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +00001108 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001109 std::string Name = Mang->getValueName(Personalities[i]);
1110 decorateName(Name, Personalities[i]);
1111 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +00001112 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001113 }
1114
Chris Lattner2e860262009-06-24 18:24:09 +00001115 // Output stubs for dynamically-linked functions
1116 if (!FnStubs.empty()) {
1117 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
1118 I != E; ++I) {
1119 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1120 "self_modifying_code+pure_instructions,5", 0);
1121 const char *Name = I->getKeyData();
1122 printSuffixedName(Name, "$stub");
1123 O << ":\n"
1124 "\t.indirect_symbol " << Name << "\n"
1125 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1126 }
1127 O << '\n';
1128 }
1129
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001130 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001131 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001132 SwitchToDataSection(
1133 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001134 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1135 I != E; ++I) {
1136 const char *Name = I->getKeyData();
1137 printSuffixedName(Name, "$non_lazy_ptr");
1138 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1139 }
1140 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001141
Evan Chenga65854f2008-12-05 01:06:39 +00001142 if (!HiddenGVStubs.empty()) {
1143 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001144 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001145 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1146 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001147 const char *Name = I->getKeyData();
1148 printSuffixedName(Name, "$non_lazy_ptr");
1149 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1150 }
Evan Chenga65854f2008-12-05 01:06:39 +00001151 }
1152
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001153 // Funny Darwin hack: This flag tells the linker that no global symbols
1154 // contain code that falls through to other global symbols (e.g. the obvious
1155 // implementation of multiple entry points). If this doesn't occur, the
1156 // linker can safely perform dead code stripping. Since LLVM never
1157 // generates code that does this, it is always safe to set.
1158 O << "\t.subsections_via_symbols\n";
1159 } else if (Subtarget->isTargetCygMing()) {
1160 // Emit type information for external functions
1161 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1162 i != e; ++i) {
1163 O << "\t.def\t " << i->getKeyData()
1164 << ";\t.scl\t" << COFF::C_EXT
1165 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1166 << ";\t.endef\n";
1167 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001168 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001169
Chris Lattnerdb191f02009-06-24 18:52:01 +00001170
1171 // Output linker support code for dllexported globals on windows.
1172 if (!DLLExportedGVs.empty()) {
1173 SwitchToDataSection(".section .drectve");
1174
1175 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1176 e = DLLExportedGVs.end(); i != e; ++i)
1177 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1178 }
1179
1180 if (!DLLExportedFns.empty()) {
1181 SwitchToDataSection(".section .drectve");
1182
1183 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1184 e = DLLExportedFns.end();
1185 i != e; ++i)
1186 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1187 }
1188
Chris Lattnerdb191f02009-06-24 18:52:01 +00001189 // Do common shutdown.
1190 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001191
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001192 if (NewAsmPrinter) {
1193 Streamer->Finish();
1194
1195 delete Streamer;
1196 delete Context;
1197 Streamer = 0;
1198 Context = 0;
1199 }
1200
Chris Lattnerdb191f02009-06-24 18:52:01 +00001201 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001202}
1203
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204// Include the auto-generated portion of the assembly writer.
1205#include "X86GenAsmWriter.inc"