blob: a7e8464e68d7c694318fc1639f437e37e3e48610 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000029#include "llvm/MC/MCContext.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000030#include "llvm/MC/MCInst.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000031#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000032#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000034#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000036#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Target/TargetAsmInfo.h"
38#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039using namespace llvm;
40
41STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
Chris Lattner19b1bd52009-06-19 00:47:33 +000043static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
44 cl::Hidden);
45
Evan Cheng0729ccf2008-01-05 00:41:47 +000046static std::string getPICLabelString(unsigned FnNum,
47 const TargetAsmInfo *TAI,
48 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 std::string label;
50 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000051 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000053 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 else
55 assert(0 && "Don't know how to print PIC label!\n");
56
57 return label;
58}
59
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000060static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
61 const TargetData *TD) {
62 X86MachineFunctionInfo Info;
63 uint64_t Size = 0;
64
65 switch (F->getCallingConv()) {
66 case CallingConv::X86_StdCall:
67 Info.setDecorationStyle(StdCall);
68 break;
69 case CallingConv::X86_FastCall:
70 Info.setDecorationStyle(FastCall);
71 break;
72 default:
73 return Info;
74 }
75
76 unsigned argNum = 1;
77 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
78 AI != AE; ++AI, ++argNum) {
79 const Type* Ty = AI->getType();
80
81 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000082 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000083 Ty = cast<PointerType>(Ty)->getElementType();
84
85 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000086 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000087 }
88
89 // We're not supporting tooooo huge arguments :)
90 Info.setBytesToPopOnReturn((unsigned int)Size);
91 return Info;
92}
93
94/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +000095/// Don't print things like \\n or \\0.
Owen Anderson847b99b2008-08-21 00:14:44 +000096static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000097 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
98 Name != E; ++Name)
99 if (isprint(*Name))
100 OS << *Name;
101}
102
103/// decorateName - Query FunctionInfoMap and use this information for various
104/// name decoration.
105void X86ATTAsmPrinter::decorateName(std::string &Name,
106 const GlobalValue *GV) {
107 const Function *F = dyn_cast<Function>(GV);
108 if (!F) return;
109
110 // We don't want to decorate non-stdcall or non-fastcall functions right now
111 unsigned CC = F->getCallingConv();
112 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
113 return;
114
115 // Decorate names only when we're targeting Cygwin/Mingw32 targets
116 if (!Subtarget->isTargetCygMing())
117 return;
118
119 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
120
121 const X86MachineFunctionInfo *Info;
122 if (info_item == FunctionInfoMap.end()) {
123 // Calculate apropriate function info and populate map
124 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
125 Info = &FunctionInfoMap[F];
126 } else {
127 Info = &info_item->second;
128 }
129
130 const FunctionType *FT = F->getFunctionType();
131 switch (Info->getDecorationStyle()) {
132 case None:
133 break;
134 case StdCall:
135 // "Pure" variadic functions do not receive @0 suffix.
136 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
137 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
138 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
139 break;
140 case FastCall:
141 // "Pure" variadic functions do not receive @0 suffix.
142 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
143 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
144 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
145
146 if (Name[0] == '_') {
147 Name[0] = '@';
148 } else {
149 Name = '@' + Name;
150 }
151 break;
152 default:
153 assert(0 && "Unsupported DecorationStyle");
154 }
155}
156
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000157void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000160 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000162 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000163
Devang Patel93698d92008-10-01 23:18:38 +0000164 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000165 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000166 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 switch (F->getLinkage()) {
168 default: assert(0 && "Unknown linkage type!");
169 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000170 case Function::PrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000171 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 break;
173 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000178 case Function::LinkOnceAnyLinkage:
179 case Function::LinkOnceODRLinkage:
180 case Function::WeakAnyLinkage:
181 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000182 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000184 O << "\t.globl\t" << CurrentFnName << '\n';
185 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << CurrentFnName << "\n"
188 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000190 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 }
192 break;
193 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000194
195 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
197 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000198 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 else if (Subtarget->isTargetCygMing()) {
200 O << "\t.def\t " << CurrentFnName
201 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000202 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
204 << ";\t.endef\n";
205 }
206
207 O << CurrentFnName << ":\n";
208 // Add some workaround for linkonce linkage on Cygwin\MinGW
209 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000210 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000212}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000219 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000220 unsigned CC = F->getCallingConv();
221
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000222 SetupMachineFunction(MF);
223 O << "\n\n";
224
225 // Populate function information map. Actually, We don't want to populate
226 // non-stdcall or non-fastcall functions' information right now.
227 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
228 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
229
230 // Print out constants referenced by the function
231 EmitConstantPool(MF.getConstantPool());
232
233 if (F->hasDLLExportLinkage())
234 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
235
236 // Print the 'header' of function
237 emitFunctionHeader(MF);
238
239 // Emit pre-function debug and/or EH information.
240 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000241 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000244 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246 I != E; ++I) {
247 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000248 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
249 // This is an entry block or a block that's only reachable via a
250 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
251 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000252 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 O << '\n';
254 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000255 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
256 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000258 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000259 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 printMachineInstruction(II);
261 }
262 }
263
Dale Johannesenf35771f2008-04-08 00:37:56 +0000264 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
265 // If the function is empty, then we need to emit *something*. Otherwise,
266 // the function's label might be associated with something that it wasn't
267 // meant to be associated with. We emit a noop in this situation.
268 // We are assuming inline asms are code.
269 O << "\tnop\n";
270 }
271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000273 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000275 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000276 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000277 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
279 // Print out jump tables referenced by the function.
280 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000281
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000282 O.flush();
283
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 // We didn't modify anything.
285 return false;
286}
287
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000288static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
290}
291
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000292static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
293 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
294 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
295}
296
297static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
299}
300
Chris Lattner357a0ca2009-06-20 19:34:09 +0000301/// print_pcrel_imm - This is used to print an immediate value that ends up
302/// being encoded as a pc-relative value. These print slightly differently, for
303/// example, a $ is not emitted.
304void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
305 const MachineOperand &MO = MI->getOperand(OpNo);
306 switch (MO.getType()) {
307 default: assert(0 && "Unknown pcrel immediate operand");
308 case MachineOperand::MO_Immediate:
309 O << MO.getImm();
310 return;
311 case MachineOperand::MO_MachineBasicBlock:
312 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
313 return;
314
315 case MachineOperand::MO_GlobalAddress: {
316 const GlobalValue *GV = MO.getGlobal();
317 std::string Name = Mang->getValueName(GV);
318 decorateName(Name, GV);
319
320 bool needCloseParen = false;
321 if (Name[0] == '$') {
322 // The name begins with a dollar-sign. In order to avoid having it look
323 // like an integer immediate to the assembler, enclose it in parens.
324 O << '(';
325 needCloseParen = true;
326 }
327
328 if (shouldPrintStub(TM, Subtarget)) {
329 // Link-once, declaration, or Weakly-linked global variables need
330 // non-lazily-resolved stubs
331 if (GV->isDeclaration() || GV->isWeakForLinker()) {
332 // Dynamically-resolved functions need a stub for the function.
333 if (isa<Function>(GV)) {
334 // Function stubs are no longer needed for Mac OS X 10.5 and up.
335 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
336 O << Name;
337 } else {
338 FnStubs.insert(Name);
339 printSuffixedName(Name, "$stub");
340 }
341 } else if (GV->hasHiddenVisibility()) {
342 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
343 // Definition is not definitely in the current translation unit.
344 O << Name;
345 else {
346 HiddenGVStubs.insert(Name);
347 printSuffixedName(Name, "$non_lazy_ptr");
348 }
349 } else {
350 GVStubs.insert(Name);
351 printSuffixedName(Name, "$non_lazy_ptr");
352 }
353 } else {
354 if (GV->hasDLLImportLinkage())
355 O << "__imp_";
356 O << Name;
357 }
358 } else {
359 if (GV->hasDLLImportLinkage()) {
360 O << "__imp_";
361 }
362 O << Name;
363
364 if (shouldPrintPLT(TM, Subtarget)) {
365 // Assemble call via PLT for externally visible symbols
366 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
367 !GV->hasLocalLinkage())
368 O << "@PLT";
369 }
370 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
371 // Save function name for later type emission
372 FnStubs.insert(Name);
373 }
374
Chris Lattner357a0ca2009-06-20 19:34:09 +0000375 printOffset(MO.getOffset());
376
377 if (needCloseParen)
378 O << ')';
379 return;
380 }
381
382 case MachineOperand::MO_ExternalSymbol: {
383 bool needCloseParen = false;
384 std::string Name(TAI->getGlobalPrefix());
385 Name += MO.getSymbolName();
386 // Print function stub suffix unless it's Mac OS X 10.5 and up.
387 if (shouldPrintStub(TM, Subtarget) &&
388 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
389 FnStubs.insert(Name);
390 printSuffixedName(Name, "$stub");
391 return;
392 }
393
394 if (Name[0] == '$') {
395 // The name begins with a dollar-sign. In order to avoid having it look
396 // like an integer immediate to the assembler, enclose it in parens.
397 O << '(';
398 needCloseParen = true;
399 }
400
401 O << Name;
402
403 if (shouldPrintPLT(TM, Subtarget)) {
404 std::string GOTName(TAI->getGlobalPrefix());
405 GOTName+="_GLOBAL_OFFSET_TABLE_";
406 if (Name == GOTName)
407 // HACK! Emit extra offset to PC during printing GOT offset to
408 // compensate for the size of popl instruction. The resulting code
409 // should look like:
410 // call .piclabel
411 // piclabel:
412 // popl %some_register
413 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
414 O << " + [.-"
415 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
416
417 O << "@PLT";
418 }
419
420 if (needCloseParen)
421 O << ')';
422
423 return;
424 }
425 }
426}
427
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
429 const char *Modifier, bool NotRIPRel) {
430 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 switch (MO.getType()) {
432 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000433 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 "Virtual registers should not make it this far!");
435 O << '%';
436 unsigned Reg = MO.getReg();
437 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000438 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
440 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
441 Reg = getX86SubSuperRegister(Reg, VT);
442 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000443 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 return;
445 }
446
447 case MachineOperand::MO_Immediate:
Evan Cheng0af5a042009-03-12 18:15:39 +0000448 if (!Modifier || (strcmp(Modifier, "debug") &&
Chris Lattner357a0ca2009-06-20 19:34:09 +0000449 strcmp(Modifier, "mem")))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000451 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 case MachineOperand::MO_JumpTableIndex: {
454 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
455 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000456 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000457 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458
459 if (TM.getRelocationModel() == Reloc::PIC_) {
460 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000461 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
462 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 else if (Subtarget->isPICStyleGOT())
464 O << "@GOTOFF";
465 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000466
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
468 O << "(%rip)";
469 return;
470 }
471 case MachineOperand::MO_ConstantPoolIndex: {
472 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
473 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000474 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000475 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476
477 if (TM.getRelocationModel() == Reloc::PIC_) {
478 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000479 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
480 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 else if (Subtarget->isPICStyleGOT())
482 O << "@GOTOFF";
483 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000484
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000485 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486
487 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
488 O << "(%rip)";
489 return;
490 }
491 case MachineOperand::MO_GlobalAddress: {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000492 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 bool needCloseParen = false;
494
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000495 const GlobalValue *GV = MO.getGlobal();
496 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
497 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000498 // If GV is an alias then use the aliasee for determining
499 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000500 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000501 GVar =dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000502 }
503
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 bool isThreadLocal = GVar && GVar->isThreadLocal();
505
506 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000507 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000508
Chris Lattner357a0ca2009-06-20 19:34:09 +0000509 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 O << '$';
511 else if (Name[0] == '$') {
512 // The name begins with a dollar-sign. In order to avoid having it look
513 // like an integer immediate to the assembler, enclose it in parens.
514 O << '(';
515 needCloseParen = true;
516 }
517
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000518 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 // Link-once, declaration, or Weakly-linked global variables need
520 // non-lazily-resolved stubs
Duncan Sands19d161f2009-03-07 15:45:40 +0000521 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 // Dynamically-resolved functions need a stub for the function.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000523 if (GV->hasHiddenVisibility()) {
Evan Chenga65854f2008-12-05 01:06:39 +0000524 if (!GV->isDeclaration() && !GV->hasCommonLinkage())
525 // Definition is not definitely in the current translation unit.
526 O << Name;
527 else {
528 HiddenGVStubs.insert(Name);
529 printSuffixedName(Name, "$non_lazy_ptr");
530 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 } else {
532 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000533 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 }
535 } else {
536 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000537 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 O << Name;
539 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000540
Chris Lattner357a0ca2009-06-20 19:34:09 +0000541 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000542 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 } else {
Chris Lattner4b06b6b2009-06-21 01:27:55 +0000544 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000545 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 }
548
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000549 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550
Chris Lattnerdabfe572009-06-21 02:22:53 +0000551 if (needCloseParen)
552 O << ')';
553
554 bool isRIPRelative = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 if (isThreadLocal) {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000556 TLSModel::Model model = getTLSModel(GVar, TM.getRelocationModel());
557 switch (model) {
558 case TLSModel::GeneralDynamic:
559 O << "@TLSGD";
560 break;
561 case TLSModel::LocalDynamic:
562 // O << "@TLSLD"; // local dynamic not implemented
Bill Wendlingc1946742009-05-30 01:09:53 +0000563 O << "@TLSGD";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000564 break;
565 case TLSModel::InitialExec:
Rafael Espindolab93a5122009-04-13 13:02:49 +0000566 if (Subtarget->is64Bit()) {
567 assert (!NotRIPRel);
Chris Lattnerdabfe572009-06-21 02:22:53 +0000568 O << "@GOTTPOFF";
569 isRIPRelative = true;
Rafael Espindolab93a5122009-04-13 13:02:49 +0000570 } else {
Rafael Espindola7b620af2009-02-27 13:37:18 +0000571 O << "@INDNTPOFF";
Rafael Espindolab93a5122009-04-13 13:02:49 +0000572 }
Rafael Espindola7b620af2009-02-27 13:37:18 +0000573 break;
574 case TLSModel::LocalExec:
575 if (Subtarget->is64Bit())
Rafael Espindolab93a5122009-04-13 13:02:49 +0000576 O << "@TPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000577 else
Bill Wendlingc1946742009-05-30 01:09:53 +0000578 O << "@NTPOFF";
Rafael Espindola7b620af2009-02-27 13:37:18 +0000579 break;
580 default:
581 assert (0 && "Unknown TLS model");
582 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000584 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
586 O << "@GOT";
587 else
588 O << "@GOTOFF";
Chris Lattnerdabfe572009-06-21 02:22:53 +0000589 } else if (Subtarget->isPICStyleRIPRel() &&
590 !NotRIPRel) {
Dan Gohmane254f322009-03-14 02:33:41 +0000591 if (TM.getRelocationModel() != Reloc::Static) {
592 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
593 O << "@GOTPCREL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 }
Chris Lattnerdabfe572009-06-21 02:22:53 +0000595
596 isRIPRelative = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 }
598 }
599
Chris Lattnerdabfe572009-06-21 02:22:53 +0000600 // Use rip when possible to reduce code size, except when
601 // index or base register are also part of the address. e.g.
602 // foo(%rip)(%rcx,%rax,4) is not legal.
603 if (isRIPRelative)
604 O << "(%rip)";
605
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 return;
607 }
608 case MachineOperand::MO_ExternalSymbol: {
Dan Gohman96f096e2009-04-23 00:57:37 +0000609 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 bool needCloseParen = false;
611 std::string Name(TAI->getGlobalPrefix());
612 Name += MO.getSymbolName();
Chris Lattner357a0ca2009-06-20 19:34:09 +0000613
Evan Chengdfd884e2008-09-20 00:13:45 +0000614 // Print function stub suffix unless it's Mac OS X 10.5 and up.
Chris Lattner357a0ca2009-06-20 19:34:09 +0000615 if (!isMemOp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 O << '$';
617 else if (Name[0] == '$') {
618 // The name begins with a dollar-sign. In order to avoid having it look
619 // like an integer immediate to the assembler, enclose it in parens.
620 O << '(';
621 needCloseParen = true;
622 }
623
624 O << Name;
625
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000626 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 std::string GOTName(TAI->getGlobalPrefix());
628 GOTName+="_GLOBAL_OFFSET_TABLE_";
629 if (Name == GOTName)
630 // HACK! Emit extra offset to PC during printing GOT offset to
631 // compensate for the size of popl instruction. The resulting code
632 // should look like:
633 // call .piclabel
634 // piclabel:
635 // popl %some_register
636 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
637 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000638 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 }
640
641 if (needCloseParen)
642 O << ')';
643
Chris Lattner357a0ca2009-06-20 19:34:09 +0000644 if (Subtarget->isPICStyleRIPRel())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 O << "(%rip)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 return;
647 }
648 default:
649 O << "<unknown operand type>"; return;
650 }
651}
652
653void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000654 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 assert(value <= 7 && "Invalid ssecc argument!");
656 switch (value) {
657 case 0: O << "eq"; break;
658 case 1: O << "lt"; break;
659 case 2: O << "le"; break;
660 case 3: O << "unord"; break;
661 case 4: O << "neq"; break;
662 case 5: O << "nlt"; break;
663 case 6: O << "nle"; break;
664 case 7: O << "ord"; break;
665 }
666}
667
Rafael Espindolabca99f72009-04-08 21:14:34 +0000668void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000669 const char *Modifier,
670 bool NotRIPRel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 MachineOperand BaseReg = MI->getOperand(Op);
672 MachineOperand IndexReg = MI->getOperand(Op+2);
673 const MachineOperand &DispSpec = MI->getOperand(Op+3);
674
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000675 NotRIPRel |= IndexReg.getReg() || BaseReg.getReg();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000676 if (DispSpec.isGlobal() ||
677 DispSpec.isCPI() ||
Dan Gohman96f096e2009-04-23 00:57:37 +0000678 DispSpec.isJTI() ||
679 DispSpec.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 printOperand(MI, Op+3, "mem", NotRIPRel);
681 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000682 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
684 O << DispVal;
685 }
686
687 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000688 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000690
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 // There are cases where we can end up with ESP/RSP in the indexreg slot.
692 // If this happens, swap the base/index register to support assemblers that
693 // don't work when the index is *SP.
694 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
695 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
696 std::swap(BaseReg, IndexReg);
697 std::swap(BaseRegOperand, IndexRegOperand);
698 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000699
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000700 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 if (BaseReg.getReg())
702 printOperand(MI, Op+BaseRegOperand, Modifier);
703
704 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000705 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 printOperand(MI, Op+IndexRegOperand, Modifier);
707 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000708 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000710 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
712}
713
Rafael Espindolabca99f72009-04-08 21:14:34 +0000714void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000715 const char *Modifier, bool NotRIPRel){
Rafael Espindolabca99f72009-04-08 21:14:34 +0000716 assert(isMem(MI, Op) && "Invalid memory reference!");
717 MachineOperand Segment = MI->getOperand(Op+4);
718 if (Segment.getReg()) {
719 printOperand(MI, Op+4, Modifier);
720 O << ':';
721 }
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000722 printLeaMemReference(MI, Op, Modifier, NotRIPRel);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000723}
724
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000725void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000726 const MachineBasicBlock *MBB) const {
727 if (!TAI->getSetDirective())
728 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000729
730 // We don't need .set machinery if we have GOT-style relocations
731 if (Subtarget->isPICStyleGOT())
732 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000733
Evan Cheng6fb06762007-11-09 01:32:10 +0000734 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
735 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000736 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000737 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000738 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000739 << '_' << uid << '\n';
740 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000741 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000742}
743
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000745 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000746 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747}
748
749
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000750void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
751 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000752 unsigned uid) const
753{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000754 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
755 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
756
757 O << JTEntryDirective << ' ';
758
759 if (TM.getRelocationModel() == Reloc::PIC_) {
760 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
761 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
762 << '_' << uid << "_set_" << MBB->getNumber();
763 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000764 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000765 O << "@GOTOFF";
766 } else
767 assert(0 && "Don't know how to print MBB label for this PIC mode");
768 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000769 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000770}
771
Chris Lattner8bb96e42009-06-15 04:42:32 +0000772bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 unsigned Reg = MO.getReg();
774 switch (Mode) {
775 default: return true; // Unknown mode.
776 case 'b': // Print QImode register
777 Reg = getX86SubSuperRegister(Reg, MVT::i8);
778 break;
779 case 'h': // Print QImode high register
780 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
781 break;
782 case 'w': // Print HImode register
783 Reg = getX86SubSuperRegister(Reg, MVT::i16);
784 break;
785 case 'k': // Print SImode register
786 Reg = getX86SubSuperRegister(Reg, MVT::i32);
787 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000788 case 'q': // Print DImode register
789 Reg = getX86SubSuperRegister(Reg, MVT::i64);
790 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 }
792
Evan Cheng00d04a72008-07-07 22:21:06 +0000793 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 return false;
795}
796
797/// PrintAsmOperand - Print out an operand for an inline asm expression.
798///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000799bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000800 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 const char *ExtraCode) {
802 // Does this asm operand have a single letter operand modifier?
803 if (ExtraCode && ExtraCode[0]) {
804 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000805
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 switch (ExtraCode[0]) {
807 default: return true; // Unknown modifier.
808 case 'c': // Don't print "$" before a global var name or constant.
Dan Gohmane254f322009-03-14 02:33:41 +0000809 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 return false;
811 case 'b': // Print QImode register
812 case 'h': // Print QImode high register
813 case 'w': // Print HImode register
814 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000815 case 'q': // Print DImode register
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000816 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
818 printOperand(MI, OpNo);
819 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000820
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000822 printOperand(MI, OpNo, "mem", /*NotRIPRel=*/true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 return false;
824 }
825 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000826
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 printOperand(MI, OpNo);
828 return false;
829}
830
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000831bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000833 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000835 if (ExtraCode && ExtraCode[0]) {
836 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000837
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000838 switch (ExtraCode[0]) {
839 default: return true; // Unknown modifier.
840 case 'b': // Print QImode register
841 case 'h': // Print QImode high register
842 case 'w': // Print HImode register
843 case 'k': // Print SImode register
844 case 'q': // Print SImode register
845 // These only apply to registers, ignore on mem.
846 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000847 case 'P': // Don't print @PLT, but do print as memory.
Anton Korobeynikov0cdf2462009-04-28 21:49:33 +0000848 printMemReference(MI, OpNo, "mem", /*NotRIPRel=*/true);
Chris Lattnerd1595722009-01-23 22:33:40 +0000849 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000850 }
851 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 printMemReference(MI, OpNo);
853 return false;
854}
855
Chris Lattnerf5da5902009-06-20 07:03:18 +0000856static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
857 // Convert registers in the addr mode according to subreg64.
858 for (unsigned i = 0; i != 4; ++i) {
859 if (!MI->getOperand(i).isReg()) continue;
860
861 unsigned Reg = MI->getOperand(i).getReg();
862 if (Reg == 0) continue;
863
864 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
865 }
866}
867
Bill Wendlingb3b11262009-02-06 21:45:08 +0000868/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
869/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870///
871void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
872 ++EmittedInsts;
873
Chris Lattner19b1bd52009-06-19 00:47:33 +0000874 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000875 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
876 O << "\t";
877 printInlineAsm(MI);
878 return;
879 } else if (MI->isLabel()) {
880 printLabel(MI);
881 return;
882 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
883 printDeclare(MI);
884 return;
885 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
886 printImplicitDef(MI);
887 return;
888 }
889
Chris Lattnere67184e2009-06-19 23:59:57 +0000890 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000891 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000892
893 TmpInst.setOpcode(MI->getOpcode());
894
895 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
896 const MachineOperand &MO = MI->getOperand(i);
897
898 MCOperand MCOp;
899 if (MO.isReg()) {
900 MCOp.MakeReg(MO.getReg());
901 } else if (MO.isImm()) {
902 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000903 } else if (MO.isMBB()) {
904 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000905 } else {
906 assert(0 && "Unimp");
907 }
908
909 TmpInst.addOperand(MCOp);
910 }
911
Chris Lattner6b9f7742009-06-20 07:59:10 +0000912 switch (TmpInst.getOpcode()) {
913 case X86::LEA64_32r:
914 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000915 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000916 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000917 }
918
Chris Lattner19b1bd52009-06-19 00:47:33 +0000919 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000920 printInstruction(&TmpInst);
921 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000922 }
923
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 // Call the autogenerated instruction printer routines.
925 printInstruction(MI);
926}
927
Devang Patelc20079e2009-06-20 01:00:07 +0000928/// doInitialization
929bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000930 if (NewAsmPrinter) {
931 Context = new MCContext();
932 // FIXME: Send this to "O" instead of outs(). For now, we force it to
933 // stdout to make it easy to compare.
934 Streamer = createAsmStreamer(*Context, outs());
935 }
936
Devang Patelc20079e2009-06-20 01:00:07 +0000937 return AsmPrinter::doInitialization(M);
938}
939
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000940void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000941 const TargetData *TD = TM.getTargetData();
942
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000943 if (!GVar->hasInitializer())
944 return; // External global require no code
945
946 // Check to see if this is a special global used by LLVM, if so, emit it.
947 if (EmitSpecialLLVMGlobal(GVar)) {
948 if (Subtarget->isTargetDarwin() &&
949 TM.getRelocationModel() == Reloc::Static) {
950 if (GVar->getName() == "llvm.global_ctors")
951 O << ".reference .constructors_used\n";
952 else if (GVar->getName() == "llvm.global_dtors")
953 O << ".reference .destructors_used\n";
954 }
955 return;
956 }
957
958 std::string name = Mang->getValueName(GVar);
959 Constant *C = GVar->getInitializer();
960 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000961 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000962 unsigned Align = TD->getPreferredAlignmentLog(GVar);
963
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000964 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000965
966 if (Subtarget->isTargetELF())
967 O << "\t.type\t" << name << ",@object\n";
968
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000969 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000970
Evan Chengcf84b142009-02-18 02:19:52 +0000971 if (C->isNullValue() && !GVar->hasSection() &&
972 !(Subtarget->isTargetDarwin() &&
973 TAI->SectionKindForGlobal(GVar) == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000974 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000975 if (GVar->hasExternalLinkage()) {
976 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000977 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000978 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000979 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000980 return;
981 }
982 }
983
984 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000985 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000986 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000987
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000988 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000989 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000990 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000991 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000992 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000993 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000994 O << "\t.globl " << name << '\n'
995 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000996 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000997 O << name << ":";
998 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000999 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001000 PrintUnmangledNameSafely(GVar, O);
1001 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001002 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001003 EmitGlobalConstant(C);
1004 return;
1005 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001006 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +00001007 if (TAI->getCOMMDirectiveTakesAlignment())
1008 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001009 }
1010 } else {
1011 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001012 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001013 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001014 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001015 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001016 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001017 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001018 }
Evan Cheng11db8142009-03-24 00:17:40 +00001019 if (VerboseAsm) {
1020 O << "\t\t" << TAI->getCommentString() << ' ';
1021 PrintUnmangledNameSafely(GVar, O);
1022 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001023 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001024 return;
1025 }
1026 }
1027
1028 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001029 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001030 case GlobalValue::LinkOnceAnyLinkage:
1031 case GlobalValue::LinkOnceODRLinkage:
1032 case GlobalValue::WeakAnyLinkage:
1033 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001034 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001035 O << "\t.globl " << name << '\n'
1036 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001037 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001038 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001039 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001040 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001041 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001042 }
1043 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001044 case GlobalValue::DLLExportLinkage:
1045 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001046 // FIXME: appending linkage variables should go into a section of
1047 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +00001048 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001049 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001050 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001051 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001052 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001053 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001054 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001055 default:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001056 assert(0 && "Unknown linkage type!");
1057 }
1058
1059 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001060 O << name << ":";
1061 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +00001062 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +00001063 PrintUnmangledNameSafely(GVar, O);
1064 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001065 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001066 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001067 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001068
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001069 EmitGlobalConstant(C);
1070}
1071
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001072bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001073 // Print out module-level global variables here.
1074 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001075 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001076 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001077
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001078 if (I->hasDLLExportLinkage())
1079 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001080 }
1081
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001082 if (Subtarget->isTargetDarwin()) {
1083 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +00001084
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001085 // Add the (possibly multiple) personalities to the set of global value
1086 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001087 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001088 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1089 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
1090 if (Personalities[i] == 0)
Evan Cheng76443dc2008-07-08 00:55:58 +00001091 continue;
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001092 std::string Name = Mang->getValueName(Personalities[i]);
1093 decorateName(Name, Personalities[i]);
1094 GVStubs.insert(Name);
Evan Cheng76443dc2008-07-08 00:55:58 +00001095 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001096 }
1097
Chris Lattner2e860262009-06-24 18:24:09 +00001098 // Output stubs for dynamically-linked functions
1099 if (!FnStubs.empty()) {
1100 for (StringSet<>::iterator I = FnStubs.begin(), E = FnStubs.end();
1101 I != E; ++I) {
1102 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1103 "self_modifying_code+pure_instructions,5", 0);
1104 const char *Name = I->getKeyData();
1105 printSuffixedName(Name, "$stub");
1106 O << ":\n"
1107 "\t.indirect_symbol " << Name << "\n"
1108 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1109 }
1110 O << '\n';
1111 }
1112
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001113 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001114 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001115 SwitchToDataSection(
1116 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattner2e860262009-06-24 18:24:09 +00001117 for (StringSet<>::iterator I = GVStubs.begin(), E = GVStubs.end();
1118 I != E; ++I) {
1119 const char *Name = I->getKeyData();
1120 printSuffixedName(Name, "$non_lazy_ptr");
1121 O << ":\n\t.indirect_symbol " << Name << "\n\t.long\t0\n";
1122 }
1123 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001124
Evan Chenga65854f2008-12-05 01:06:39 +00001125 if (!HiddenGVStubs.empty()) {
1126 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001127 EmitAlignment(2);
Chris Lattner2e860262009-06-24 18:24:09 +00001128 for (StringSet<>::iterator I = HiddenGVStubs.begin(),
1129 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner2e860262009-06-24 18:24:09 +00001130 const char *Name = I->getKeyData();
1131 printSuffixedName(Name, "$non_lazy_ptr");
1132 O << ":\n" << TAI->getData32bitsDirective() << Name << '\n';
1133 }
Evan Chenga65854f2008-12-05 01:06:39 +00001134 }
1135
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001136 // Funny Darwin hack: This flag tells the linker that no global symbols
1137 // contain code that falls through to other global symbols (e.g. the obvious
1138 // implementation of multiple entry points). If this doesn't occur, the
1139 // linker can safely perform dead code stripping. Since LLVM never
1140 // generates code that does this, it is always safe to set.
1141 O << "\t.subsections_via_symbols\n";
1142 } else if (Subtarget->isTargetCygMing()) {
1143 // Emit type information for external functions
1144 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1145 i != e; ++i) {
1146 O << "\t.def\t " << i->getKeyData()
1147 << ";\t.scl\t" << COFF::C_EXT
1148 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1149 << ";\t.endef\n";
1150 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001151 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001152
Chris Lattnerdb191f02009-06-24 18:52:01 +00001153
1154 // Output linker support code for dllexported globals on windows.
1155 if (!DLLExportedGVs.empty()) {
1156 SwitchToDataSection(".section .drectve");
1157
1158 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1159 e = DLLExportedGVs.end(); i != e; ++i)
1160 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
1161 }
1162
1163 if (!DLLExportedFns.empty()) {
1164 SwitchToDataSection(".section .drectve");
1165
1166 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1167 e = DLLExportedFns.end();
1168 i != e; ++i)
1169 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1170 }
1171
Chris Lattnerdb191f02009-06-24 18:52:01 +00001172 // Do common shutdown.
1173 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +00001174
Chris Lattner7cf8daf2009-06-24 05:46:28 +00001175 if (NewAsmPrinter) {
1176 Streamer->Finish();
1177
1178 delete Streamer;
1179 delete Context;
1180 Streamer = 0;
1181 Context = 0;
1182 }
1183
Chris Lattnerdb191f02009-06-24 18:52:01 +00001184 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001185}
1186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187// Include the auto-generated portion of the assembly writer.
1188#include "X86GenAsmWriter.inc"