blob: a3618751fb958f1c64648397868db50c8e77281c [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 Lattner14f791a2009-06-24 19:19:16 +000046
47void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000049 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 else if (Subtarget->isTargetELF())
Chris Lattner14f791a2009-06-24 19:19:16 +000051 O << ".Lllvm$" << getFunctionNumber() << "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 else
53 assert(0 && "Don't know how to print PIC label!\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054}
55
Chris Lattner14f791a2009-06-24 19:19:16 +000056
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000057static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
58 const TargetData *TD) {
59 X86MachineFunctionInfo Info;
60 uint64_t Size = 0;
61
62 switch (F->getCallingConv()) {
63 case CallingConv::X86_StdCall:
64 Info.setDecorationStyle(StdCall);
65 break;
66 case CallingConv::X86_FastCall:
67 Info.setDecorationStyle(FastCall);
68 break;
69 default:
70 return Info;
71 }
72
73 unsigned argNum = 1;
74 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
75 AI != AE; ++AI, ++argNum) {
76 const Type* Ty = AI->getType();
77
78 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000079 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000080 Ty = cast<PointerType>(Ty)->getElementType();
81
82 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000083 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000084 }
85
86 // We're not supporting tooooo huge arguments :)
87 Info.setBytesToPopOnReturn((unsigned int)Size);
88 return Info;
89}
90
91/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +000092/// Don't print things like \\n or \\0.
Owen Anderson847b99b2008-08-21 00:14:44 +000093static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000094 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
95 Name != E; ++Name)
96 if (isprint(*Name))
97 OS << *Name;
98}
99
100/// decorateName - Query FunctionInfoMap and use this information for various
101/// name decoration.
102void X86ATTAsmPrinter::decorateName(std::string &Name,
103 const GlobalValue *GV) {
104 const Function *F = dyn_cast<Function>(GV);
105 if (!F) return;
106
107 // We don't want to decorate non-stdcall or non-fastcall functions right now
108 unsigned CC = F->getCallingConv();
109 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
110 return;
111
112 // Decorate names only when we're targeting Cygwin/Mingw32 targets
113 if (!Subtarget->isTargetCygMing())
114 return;
115
116 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
117
118 const X86MachineFunctionInfo *Info;
119 if (info_item == FunctionInfoMap.end()) {
120 // Calculate apropriate function info and populate map
121 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
122 Info = &FunctionInfoMap[F];
123 } else {
124 Info = &info_item->second;
125 }
126
127 const FunctionType *FT = F->getFunctionType();
128 switch (Info->getDecorationStyle()) {
129 case None:
130 break;
131 case StdCall:
132 // "Pure" variadic functions do not receive @0 suffix.
133 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
134 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
135 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
136 break;
137 case FastCall:
138 // "Pure" variadic functions do not receive @0 suffix.
139 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
140 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
141 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
142
143 if (Name[0] == '_') {
144 Name[0] = '@';
145 } else {
146 Name = '@' + Name;
147 }
148 break;
149 default:
150 assert(0 && "Unsupported DecorationStyle");
151 }
152}
153
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000154void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000157 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000159 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000160
Devang Patel93698d92008-10-01 23:18:38 +0000161 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000162 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000163 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 switch (F->getLinkage()) {
165 default: assert(0 && "Unknown linkage type!");
166 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000167 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000168 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 break;
170 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000173 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000175 case Function::LinkOnceAnyLinkage:
176 case Function::LinkOnceODRLinkage:
177 case Function::WeakAnyLinkage:
178 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000179 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << CurrentFnName << '\n';
182 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000184 O << "\t.globl\t" << CurrentFnName << "\n"
185 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000187 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 }
189 break;
190 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000191
192 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
194 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000195 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 else if (Subtarget->isTargetCygMing()) {
197 O << "\t.def\t " << CurrentFnName
198 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000199 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
201 << ";\t.endef\n";
202 }
203
204 O << CurrentFnName << ":\n";
205 // Add some workaround for linkonce linkage on Cygwin\MinGW
206 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000207 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000209}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000211/// runOnMachineFunction - This uses the printMachineInstruction()
212/// method to print assembly for each instruction.
213///
214bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
215 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000216 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000217 unsigned CC = F->getCallingConv();
218
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000219 SetupMachineFunction(MF);
220 O << "\n\n";
221
222 // Populate function information map. Actually, We don't want to populate
223 // non-stdcall or non-fastcall functions' information right now.
224 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
225 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
226
227 // Print out constants referenced by the function
228 EmitConstantPool(MF.getConstantPool());
229
230 if (F->hasDLLExportLinkage())
231 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
232
233 // Print the 'header' of function
234 emitFunctionHeader(MF);
235
236 // Emit pre-function debug and/or EH information.
237 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000238 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000239
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000241 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
243 I != E; ++I) {
244 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000245 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
246 // This is an entry block or a block that's only reachable via a
247 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
248 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000249 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 O << '\n';
251 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000252 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
253 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000255 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000256 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 printMachineInstruction(II);
258 }
259 }
260
Dale Johannesenf35771f2008-04-08 00:37:56 +0000261 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
262 // If the function is empty, then we need to emit *something*. Otherwise,
263 // the function's label might be associated with something that it wasn't
264 // meant to be associated with. We emit a noop in this situation.
265 // We are assuming inline asms are code.
266 O << "\tnop\n";
267 }
268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000270 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000272 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000273 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000274 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275
276 // Print out jump tables referenced by the function.
277 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000278
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000279 O.flush();
280
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 // We didn't modify anything.
282 return false;
283}
284
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000285static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
287}
288
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000289static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
290 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
291 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
292}
293
294static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
296}
297
Chris Lattner357a0ca2009-06-20 19:34:09 +0000298/// print_pcrel_imm - This is used to print an immediate value that ends up
299/// being encoded as a pc-relative value. These print slightly differently, for
300/// example, a $ is not emitted.
301void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
302 const MachineOperand &MO = MI->getOperand(OpNo);
303 switch (MO.getType()) {
304 default: assert(0 && "Unknown pcrel immediate operand");
305 case MachineOperand::MO_Immediate:
306 O << MO.getImm();
307 return;
308 case MachineOperand::MO_MachineBasicBlock:
309 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
310 return;
311
312 case MachineOperand::MO_GlobalAddress: {
313 const GlobalValue *GV = MO.getGlobal();
314 std::string Name = Mang->getValueName(GV);
315 decorateName(Name, GV);
316
317 bool needCloseParen = false;
318 if (Name[0] == '$') {
319 // The name begins with a dollar-sign. In order to avoid having it look
320 // like an integer immediate to the assembler, enclose it in parens.
321 O << '(';
322 needCloseParen = true;
323 }
324
325 if (shouldPrintStub(TM, Subtarget)) {
326 // Link-once, declaration, or Weakly-linked global variables need
327 // non-lazily-resolved stubs
328 if (GV->isDeclaration() || GV->isWeakForLinker()) {
329 // Dynamically-resolved functions need a stub for the function.
330 if (isa<Function>(GV)) {
331 // Function stubs are no longer needed for Mac OS X 10.5 and up.
332 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
333 O << Name;
334 } else {
335 FnStubs.insert(Name);
336 printSuffixedName(Name, "$stub");
337 }
338 } else if (GV->hasHiddenVisibility()) {
339 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
340 // Definition is not definitely in the current translation unit.
341 O << Name;
342 else {
343 HiddenGVStubs.insert(Name);
344 printSuffixedName(Name, "$non_lazy_ptr");
345 }
346 } else {
347 GVStubs.insert(Name);
348 printSuffixedName(Name, "$non_lazy_ptr");
349 }
350 } else {
351 if (GV->hasDLLImportLinkage())
352 O << "__imp_";
353 O << Name;
354 }
355 } else {
356 if (GV->hasDLLImportLinkage()) {
357 O << "__imp_";
358 }
359 O << Name;
360
361 if (shouldPrintPLT(TM, Subtarget)) {
362 // Assemble call via PLT for externally visible symbols
363 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
364 !GV->hasLocalLinkage())
365 O << "@PLT";
366 }
367 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
368 // Save function name for later type emission
369 FnStubs.insert(Name);
370 }
371
Chris Lattner357a0ca2009-06-20 19:34:09 +0000372 printOffset(MO.getOffset());
373
374 if (needCloseParen)
375 O << ')';
376 return;
377 }
378
379 case MachineOperand::MO_ExternalSymbol: {
380 bool needCloseParen = false;
381 std::string Name(TAI->getGlobalPrefix());
382 Name += MO.getSymbolName();
383 // Print function stub suffix unless it's Mac OS X 10.5 and up.
384 if (shouldPrintStub(TM, Subtarget) &&
385 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
386 FnStubs.insert(Name);
387 printSuffixedName(Name, "$stub");
388 return;
389 }
390
391 if (Name[0] == '$') {
392 // The name begins with a dollar-sign. In order to avoid having it look
393 // like an integer immediate to the assembler, enclose it in parens.
394 O << '(';
395 needCloseParen = true;
396 }
397
398 O << Name;
399
400 if (shouldPrintPLT(TM, Subtarget)) {
401 std::string GOTName(TAI->getGlobalPrefix());
402 GOTName+="_GLOBAL_OFFSET_TABLE_";
Chris Lattner14f791a2009-06-24 19:19:16 +0000403 if (Name == GOTName) {
Chris Lattner357a0ca2009-06-20 19:34:09 +0000404 // HACK! Emit extra offset to PC during printing GOT offset to
405 // compensate for the size of popl instruction. The resulting code
406 // should look like:
407 // call .piclabel
408 // piclabel:
409 // popl %some_register
410 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Chris Lattner14f791a2009-06-24 19:19:16 +0000411 O << " + [.-";
412 PrintPICBaseSymbol();
413 O << ']';
414 }
Chris Lattner357a0ca2009-06-20 19:34:09 +0000415
416 O << "@PLT";
417 }
418
419 if (needCloseParen)
420 O << ')';
421
422 return;
423 }
424 }
425}
426
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
428 const char *Modifier, bool NotRIPRel) {
429 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 switch (MO.getType()) {
431 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000432 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 "Virtual registers should not make it this far!");
434 O << '%';
435 unsigned Reg = MO.getReg();
436 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000437 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
439 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
440 Reg = getX86SubSuperRegister(Reg, VT);
441 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000442 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 return;
444 }
445
446 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000447 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000448 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000450 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 case MachineOperand::MO_JumpTableIndex: {
453 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
454 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000455 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000456 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457
458 if (TM.getRelocationModel() == Reloc::PIC_) {
459 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000460 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
461 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 else if (Subtarget->isPICStyleGOT())
463 O << "@GOTOFF";
464 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000465
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
467 O << "(%rip)";
468 return;
469 }
470 case MachineOperand::MO_ConstantPoolIndex: {
471 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
472 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000473 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000474 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475
476 if (TM.getRelocationModel() == Reloc::PIC_) {
477 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000478 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
479 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 else if (Subtarget->isPICStyleGOT())
481 O << "@GOTOFF";
482 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000483
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000484 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485
486 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
487 O << "(%rip)";
488 return;
489 }
490 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000491 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 bool needCloseParen = false;
493
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000494 const GlobalValue *GV = MO.getGlobal();
495 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
496 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000497 // If GV is an alias then use the aliasee for determining
498 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000499 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000500 GVar =dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000501 }
502
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 bool isThreadLocal = GVar && GVar->isThreadLocal();
504
505 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000506 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000507
Chris Lattner357a0ca2009-06-20 19:34:09 +0000508 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 O << '$';
510 else if (Name[0] == '$') {
511 // The name begins with a dollar-sign. In order to avoid having it look
512 // like an integer immediate to the assembler, enclose it in parens.
513 O << '(';
514 needCloseParen = true;
515 }
516
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000517 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 // Link-once, declaration, or Weakly-linked global variables need
519 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000520 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000522 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-12-05 01:06:39 +0000523 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
524 // Definition is not definitely in the current translation unit.
525 O << Name;
526 else {
527 HiddenGVStubs.insert(Name);
528 printSuffixedName(Name, "$non_lazy_ptr");
529 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 } else {
531 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000532 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 }
534 } else {
535 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000536 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 O << Name;
538 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000539
Chris Lattner14f791a2009-06-24 19:19:16 +0000540 if (TM.getRelocationModel() == Reloc::PIC_) {
541 O << '-';
542 PrintPICBaseSymbol();
543 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000545 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000546 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 }
549
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000550 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551
Chris Lattnerdabfe572009-06-21 02:22:53 +0000552 if (needCloseParen)
553 O << ')';
554
555 bool isRIPRelative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000557 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
558 switch (model) {
559 case TLSModel::GeneralDynamic:
560 O << "@TLSGD";
561 break;
562 case TLSModel::LocalDynamic:
563 // O << "@TLSLD"; // local dynamic not implemented
Bill Wendlingc1946742009-05-30 01:09:53 +0000564 O << "@TLSGD";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000565 break;
566 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000567 if (Subtarget->is64Bit()) {
568 assert (!NotRIPRel);
Chris Lattnerdabfe572009-06-21 02:22:53 +0000569 O << "@GOTTPOFF";
570 isRIPRelative = true;
Rafael Espindolab93a5122009-04-13 13:02:49 +0000571 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000572 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000573 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000574 break;
575 case TLSModel::LocalExec:
576 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000577 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000578 else
Bill Wendlingc1946742009-05-30 01:09:53 +0000579 O << "@NTPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000580 break;
581 default:
582 assert (0 && "Unknown TLS model");
583 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000585 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
587 O << "@GOT";
588 else
589 O << "@GOTOFF";
Chris Lattnerdabfe572009-06-21 02:22:53 +0000590 } else if (Subtarget->isPICStyleRIPRel() &&
591 !NotRIPRel) {
Dan Gohmane254f322009-03-14 02:33:41 +0000592 if (TM.getRelocationModel() != Reloc::Static) {
593 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
594 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 }
Chris Lattnerdabfe572009-06-21 02:22:53 +0000596
597 isRIPRelative = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 }
599 }
600
Chris Lattnerdabfe572009-06-21 02:22:53 +0000601 // Use rip when possible to reduce code size, except when
602 // index or base register are also part of the address. e.g.
603 // foo(%rip)(%rcx,%rax,4) is not legal.
604 if (isRIPRelative)
605 O << "(%rip)";
606
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 return;
608 }
609 case MachineOperand::MO_ExternalSymbol: {
Dan Gohman96f096e2009-04-23 00:57:37 +0000610 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 bool needCloseParen = false;
612 std::string Name(TAI->getGlobalPrefix());
613 Name += MO.getSymbolName();
Chris Lattner357a0ca2009-06-20 19:34:09 +0000614
Evan Chengdfd884e2008-09-20 00:13:45 +0000615 // Print function stub suffix unless it's Mac OS X 10.5 and up.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000616 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 O << '$';
618 else if (Name[0] == '$') {
619 // The name begins with a dollar-sign. In order to avoid having it look
620 // like an integer immediate to the assembler, enclose it in parens.
621 O << '(';
622 needCloseParen = true;
623 }
624
625 O << Name;
626
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000627 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 std::string GOTName(TAI->getGlobalPrefix());
629 GOTName+="_GLOBAL_OFFSET_TABLE_";
Chris Lattner14f791a2009-06-24 19:19:16 +0000630 if (Name == GOTName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 // HACK! Emit extra offset to PC during printing GOT offset to
632 // compensate for the size of popl instruction. The resulting code
633 // should look like:
634 // call .piclabel
635 // piclabel:
636 // popl %some_register
637 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Chris Lattner14f791a2009-06-24 19:19:16 +0000638 O << " + [.-";
639 PrintPICBaseSymbol();
640 O << ']';
641 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 }
643
644 if (needCloseParen)
645 O << ')';
646
Chris Lattner357a0ca2009-06-20 19:34:09 +0000647 if (Subtarget->isPICStyleRIPRel())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 O << "(%rip)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 return;
650 }
651 default:
652 O << "<unknown operand type>"; return;
653 }
654}
655
656void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000657 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 assert(value <= 7 && "Invalid ssecc argument!");
659 switch (value) {
660 case 0: O << "eq"; break;
661 case 1: O << "lt"; break;
662 case 2: O << "le"; break;
663 case 3: O << "unord"; break;
664 case 4: O << "neq"; break;
665 case 5: O << "nlt"; break;
666 case 6: O << "nle"; break;
667 case 7: O << "ord"; break;
668 }
669}
670
Rafael Espindolabca99f72009-04-08 21:14:34 +0000671void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000672 const char *Modifier,
673 bool NotRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 MachineOperand BaseReg = MI->getOperand(Op);
675 MachineOperand IndexReg = MI->getOperand(Op+2);
676 const MachineOperand &DispSpec = MI->getOperand(Op+3);
677
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000678 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000679 if (DispSpec.isGlobal() ||
680 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000681 DispSpec.isJTI() ||
682 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 printOperand(MI, Op+3, "mem", NotRIPRel);
684 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000685 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
687 O << DispVal;
688 }
689
690 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000691 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000693
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 // There are cases where we can end up with ESP/RSP in the indexreg slot.
695 // If this happens, swap the base/index register to support assemblers that
696 // don't work when the index is *SP.
697 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
698 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
699 std::swap(BaseReg, IndexReg);
700 std::swap(BaseRegOperand, IndexRegOperand);
701 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000702
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000703 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 if (BaseReg.getReg())
705 printOperand(MI, Op+BaseRegOperand, Modifier);
706
707 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000708 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 printOperand(MI, Op+IndexRegOperand, Modifier);
710 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000711 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000713 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 }
715}
716
Rafael Espindolabca99f72009-04-08 21:14:34 +0000717void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000718 const char *Modifier, bool NotRIPRel){
Rafael Espindolabca99f72009-04-08 21:14:34 +0000719 assert(isMem(MI, Op) && "Invalid memory reference!");
720 MachineOperand Segment = MI->getOperand(Op+4);
721 if (Segment.getReg()) {
722 printOperand(MI, Op+4, Modifier);
723 O << ':';
724 }
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000725 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000726}
727
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000728void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000729 const MachineBasicBlock *MBB) const {
730 if (!TAI->getSetDirective())
731 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000732
733 // We don't need .set machinery if we have GOT-style relocations
734 if (Subtarget->isPICStyleGOT())
735 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000736
Evan Cheng6fb06762007-11-09 01:32:10 +0000737 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
738 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000739 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000740 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000741 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000742 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000743 else {
744 O << '-';
745 PrintPICBaseSymbol();
746 O << '\n';
747 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000748}
749
Chris Lattner14f791a2009-06-24 19:19:16 +0000750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000752 PrintPICBaseSymbol();
753 O << '\n';
754 PrintPICBaseSymbol();
755 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756}
757
758
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000759void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
760 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000761 unsigned uid) const
762{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000763 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
764 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
765
766 O << JTEntryDirective << ' ';
767
768 if (TM.getRelocationModel() == Reloc::PIC_) {
769 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
770 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
771 << '_' << uid << "_set_" << MBB->getNumber();
772 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000773 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000774 O << "@GOTOFF";
775 } else
776 assert(0 && "Don't know how to print MBB label for this PIC mode");
777 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000778 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000779}
780
Chris Lattner8bb96e42009-06-15 04:42:32 +0000781bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 unsigned Reg = MO.getReg();
783 switch (Mode) {
784 default: return true; // Unknown mode.
785 case 'b': // Print QImode register
786 Reg = getX86SubSuperRegister(Reg, MVT::i8);
787 break;
788 case 'h': // Print QImode high register
789 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
790 break;
791 case 'w': // Print HImode register
792 Reg = getX86SubSuperRegister(Reg, MVT::i16);
793 break;
794 case 'k': // Print SImode register
795 Reg = getX86SubSuperRegister(Reg, MVT::i32);
796 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000797 case 'q': // Print DImode register
798 Reg = getX86SubSuperRegister(Reg, MVT::i64);
799 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 }
801
Evan Cheng00d04a72008-07-07 22:21:06 +0000802 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 return false;
804}
805
806/// PrintAsmOperand - Print out an operand for an inline asm expression.
807///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000808bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000809 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 const char *ExtraCode) {
811 // Does this asm operand have a single letter operand modifier?
812 if (ExtraCode && ExtraCode[0]) {
813 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000814
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 switch (ExtraCode[0]) {
816 default: return true; // Unknown modifier.
817 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000818 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 return false;
820 case 'b': // Print QImode register
821 case 'h': // Print QImode high register
822 case 'w': // Print HImode register
823 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000824 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000825 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
827 printOperand(MI, OpNo);
828 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000829
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000831 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 return false;
833 }
834 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000835
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 printOperand(MI, OpNo);
837 return false;
838}
839
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000840bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000842 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000844 if (ExtraCode && ExtraCode[0]) {
845 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000846
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000847 switch (ExtraCode[0]) {
848 default: return true; // Unknown modifier.
849 case 'b': // Print QImode register
850 case 'h': // Print QImode high register
851 case 'w': // Print HImode register
852 case 'k': // Print SImode register
853 case 'q': // Print SImode register
854 // These only apply to registers, ignore on mem.
855 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000856 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000857 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattnerd1595722009-01-23 22:33:40 +0000858 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000859 }
860 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 printMemReference(MI, OpNo);
862 return false;
863}
864
Chris Lattnerf5da5902009-06-20 07:03:18 +0000865static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
866 // Convert registers in the addr mode according to subreg64.
867 for (unsigned i = 0; i != 4; ++i) {
868 if (!MI->getOperand(i).isReg()) continue;
869
870 unsigned Reg = MI->getOperand(i).getReg();
871 if (Reg == 0) continue;
872
873 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
874 }
875}
876
Bill Wendlingb3b11262009-02-06 21:45:08 +0000877/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
878/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879///
880void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
881 ++EmittedInsts;
882
Chris Lattner19b1bd52009-06-19 00:47:33 +0000883 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000884 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
885 O << "\t";
886 printInlineAsm(MI);
887 return;
888 } else if (MI->isLabel()) {
889 printLabel(MI);
890 return;
891 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
892 printDeclare(MI);
893 return;
894 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
895 printImplicitDef(MI);
896 return;
897 }
898
Chris Lattnere67184e2009-06-19 23:59:57 +0000899 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000900 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000901
902 TmpInst.setOpcode(MI->getOpcode());
903
904 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
905 const MachineOperand &MO = MI->getOperand(i);
906
907 MCOperand MCOp;
908 if (MO.isReg()) {
909 MCOp.MakeReg(MO.getReg());
910 } else if (MO.isImm()) {
911 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000912 } else if (MO.isMBB()) {
913 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000914 } else {
915 assert(0 && "Unimp");
916 }
917
918 TmpInst.addOperand(MCOp);
919 }
920
Chris Lattner6b9f7742009-06-20 07:59:10 +0000921 switch (TmpInst.getOpcode()) {
922 case X86::LEA64_32r:
923 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000924 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000925 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000926 }
927
Chris Lattner19b1bd52009-06-19 00:47:33 +0000928 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000929 printInstruction(&TmpInst);
930 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000931 }
932
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 // Call the autogenerated instruction printer routines.
934 printInstruction(MI);
935}
936
Devang Patelc20079e2009-06-20 01:00:07 +0000937/// doInitialization
938bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000939 if (NewAsmPrinter) {
940 Context = new MCContext();
941 // FIXME: Send this to "O" instead of outs(). For now, we force it to
942 // stdout to make it easy to compare.
943 Streamer = createAsmStreamer(*Context, outs());
944 }
945
Devang Patelc20079e2009-06-20 01:00:07 +0000946 return AsmPrinter::doInitialization(M);
947}
948
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000949void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000950 const TargetData *TD = TM.getTargetData();
951
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000952 if (!GVar->hasInitializer())
953 return; // External global require no code
954
955 // Check to see if this is a special global used by LLVM, if so, emit it.
956 if (EmitSpecialLLVMGlobal(GVar)) {
957 if (Subtarget->isTargetDarwin() &&
958 TM.getRelocationModel() == Reloc::Static) {
959 if (GVar->getName() == "llvm.global_ctors")
960 O << ".reference .constructors_used\n";
961 else if (GVar->getName() == "llvm.global_dtors")
962 O << ".reference .destructors_used\n";
963 }
964 return;
965 }
966
967 std::string name = Mang->getValueName(GVar);
968 Constant *C = GVar->getInitializer();
969 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000970 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000971 unsigned Align = TD->getPreferredAlignmentLog(GVar);
972
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000973 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000974
975 if (Subtarget->isTargetELF())
976 O << "\t.type\t" << name << ",@object\n";
977
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000978 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000979
Evan Chengcf84b142009-02-18 02:19:52 +0000980 if (C->isNullValue() && !GVar->hasSection() &&
981 !(Subtarget->isTargetDarwin() &&
982 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000983 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000984 if (GVar->hasExternalLinkage()) {
985 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000986 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000987 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000988 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000989 return;
990 }
991 }
992
993 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000994 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000995 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000996
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000997 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000998 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000999 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001000 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001001 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001002 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001003 O << "\t.globl " << name << '\n'
1004 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001005 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001006 O << name << ":";
1007 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +00001008 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001009 PrintUnmangledNameSafely(GVar, O);
1010 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001011 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001012 EmitGlobalConstant(C);
1013 return;
1014 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001015 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +00001016 if (TAI->getCOMMDirectiveTakesAlignment())
1017 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001018 }
1019 } else {
1020 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001021 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001022 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001023 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001024 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001025 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001026 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001027 }
Evan Cheng11db8142009-03-24 00:17:40 +00001028 if (VerboseAsm) {
1029 O << "\t\t" << TAI->getCommentString() << ' ';
1030 PrintUnmangledNameSafely(GVar, O);
1031 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001032 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001033 return;
1034 }
1035 }
1036
1037 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001038 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001039 case GlobalValue::LinkOnceAnyLinkage:
1040 case GlobalValue::LinkOnceODRLinkage:
1041 case GlobalValue::WeakAnyLinkage:
1042 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001043 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001044 O << "\t.globl " << name << '\n'
1045 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001046 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001047 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001048 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001049 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001050 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001051 }
1052 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001053 case GlobalValue::DLLExportLinkage:
1054 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001055 // FIXME: appending linkage variables should go into a section of
1056 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +00001057 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001058 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001059 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001060 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001061 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001062 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001063 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001064 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001065 assert(0 && "Unknown linkage type!");
1066 }
1067
1068 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001069 O << name << ":";
1070 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +00001071 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001072 PrintUnmangledNameSafely(GVar, O);
1073 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001074 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001075 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001076 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001077
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001078 EmitGlobalConstant(C);
1079}
1080
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001081bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001082 // Print out module-level global variables here.
1083 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001084 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001085 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001086
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001087 if (I->hasDLLExportLinkage())
1088 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001089 }
1090
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001091 if (Subtarget->isTargetDarwin()) {
1092 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +00001093
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001094 // Add the (possibly multiple) personalities to the set of global value
1095 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001096 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001097 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1098 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
1099 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +00001100 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001101 std::string Name = Mang->getValueName(Personalities[i]);
1102 decorateName(Name, Personalities[i]);
1103 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +00001104 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001105 }
1106
Chris Lattner2e860262009-06-24 18:24:09 +00001107 // Output stubs for dynamically-linked functions
1108 if (!FnStubs.empty()) {
1109 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
1110 I != E; ++I) {
1111 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1112 "self_modifying_code+pure_instructions,5", 0);
1113 const char *Name = I->getKeyData();
1114 printSuffixedName(Name, "$stub");
1115 O << ":\n"
1116 "\t.indirect_symbol " << Name << "\n"
1117 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1118 }
1119 O << '\n';
1120 }
1121
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001122 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001123 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001124 SwitchToDataSection(
1125 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001126 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1127 I != E; ++I) {
1128 const char *Name = I->getKeyData();
1129 printSuffixedName(Name, "$non_lazy_ptr");
1130 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1131 }
1132 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001133
Evan Chenga65854f2008-12-05 01:06:39 +00001134 if (!HiddenGVStubs.empty()) {
1135 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001136 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001137 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1138 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001139 const char *Name = I->getKeyData();
1140 printSuffixedName(Name, "$non_lazy_ptr");
1141 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1142 }
Evan Chenga65854f2008-12-05 01:06:39 +00001143 }
1144
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001145 // Funny Darwin hack: This flag tells the linker that no global symbols
1146 // contain code that falls through to other global symbols (e.g. the obvious
1147 // implementation of multiple entry points). If this doesn't occur, the
1148 // linker can safely perform dead code stripping. Since LLVM never
1149 // generates code that does this, it is always safe to set.
1150 O << "\t.subsections_via_symbols\n";
1151 } else if (Subtarget->isTargetCygMing()) {
1152 // Emit type information for external functions
1153 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1154 i != e; ++i) {
1155 O << "\t.def\t " << i->getKeyData()
1156 << ";\t.scl\t" << COFF::C_EXT
1157 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1158 << ";\t.endef\n";
1159 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001160 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001161
Chris Lattnerdb191f02009-06-24 18:52:01 +00001162
1163 // Output linker support code for dllexported globals on windows.
1164 if (!DLLExportedGVs.empty()) {
1165 SwitchToDataSection(".section .drectve");
1166
1167 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1168 e = DLLExportedGVs.end(); i != e; ++i)
1169 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1170 }
1171
1172 if (!DLLExportedFns.empty()) {
1173 SwitchToDataSection(".section .drectve");
1174
1175 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1176 e = DLLExportedFns.end();
1177 i != e; ++i)
1178 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1179 }
1180
Chris Lattnerdb191f02009-06-24 18:52:01 +00001181 // Do common shutdown.
1182 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001183
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001184 if (NewAsmPrinter) {
1185 Streamer->Finish();
1186
1187 delete Streamer;
1188 delete Context;
1189 Streamer = 0;
1190 Context = 0;
1191 }
1192
Chris Lattnerdb191f02009-06-24 18:52:01 +00001193 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001194}
1195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196// Include the auto-generated portion of the assembly writer.
1197#include "X86GenAsmWriter.inc"