blob: 8eac2df29b38c5b9d0749b75d7352ec52e0e7c9c [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
Cedric Venetd85f51a2008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000022#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000024#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000025#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000026#include "llvm/Assembly/Writer.h"
Chris Lattner2a3c20b2009-09-11 06:36:33 +000027#include "llvm/MC/MCContext.h"
Chris Lattner522e9a02009-09-02 17:35:12 +000028#include "llvm/MC/MCSectionMachO.h"
Chris Lattner2a3c20b2009-09-11 06:36:33 +000029#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000035#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000036#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattnercf1ed752009-09-11 04:28:13 +000038#include "llvm/ADT/SmallString.h"
39#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000040using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000041
Chris Lattner95b2c7d2006-12-19 22:59:26 +000042STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
Chris Lattnerab162992009-06-24 19:44:36 +000044//===----------------------------------------------------------------------===//
45// Primitive Helper Functions.
46//===----------------------------------------------------------------------===//
Chris Lattnerb5299dd2009-06-24 19:19:16 +000047
48void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner9b60e042009-08-16 04:28:14 +000049 // FIXME: the actual label generated doesn't matter here! Just mangle in
50 // something unique (the function number) with Private prefix.
Evan Cheng071b9d52007-01-18 01:49:58 +000051 if (Subtarget->isTargetDarwin())
Chris Lattnerb5299dd2009-06-24 19:19:16 +000052 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattner9b60e042009-08-16 04:28:14 +000053 else {
54 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner8529d282009-07-08 23:09:14 +000055 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattner9b60e042009-08-16 04:28:14 +000056 }
57}
58
Anton Korobeynikov75b68822008-06-28 11:08:27 +000059static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
60 const TargetData *TD) {
61 X86MachineFunctionInfo Info;
62 uint64_t Size = 0;
63
64 switch (F->getCallingConv()) {
65 case CallingConv::X86_StdCall:
66 Info.setDecorationStyle(StdCall);
67 break;
68 case CallingConv::X86_FastCall:
69 Info.setDecorationStyle(FastCall);
70 break;
71 default:
72 return Info;
73 }
74
75 unsigned argNum = 1;
76 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
77 AI != AE; ++AI, ++argNum) {
78 const Type* Ty = AI->getType();
79
80 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000081 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000082 Ty = cast<PointerType>(Ty)->getElementType();
83
84 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000085 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000086 }
87
88 // We're not supporting tooooo huge arguments :)
89 Info.setBytesToPopOnReturn((unsigned int)Size);
90 return Info;
91}
92
Chris Lattnerc08872e2009-07-15 04:55:56 +000093/// DecorateCygMingName - Query FunctionInfoMap and use this information for
94/// various name decorations for Cygwin and MingW.
Chris Lattnercf1ed752009-09-11 04:28:13 +000095void X86ATTAsmPrinter::DecorateCygMingName(SmallVectorImpl<char> &Name,
Chris Lattnerc08872e2009-07-15 04:55:56 +000096 const GlobalValue *GV) {
97 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
98
Anton Korobeynikov75b68822008-06-28 11:08:27 +000099 const Function *F = dyn_cast<Function>(GV);
100 if (!F) return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000101
Chris Lattner7a4e4642009-07-10 21:57:21 +0000102 // Save function name for later type emission.
Chris Lattnerc08872e2009-07-15 04:55:56 +0000103 if (F->isDeclaration())
Chris Lattnercf1ed752009-09-11 04:28:13 +0000104 CygMingStubs.insert(StringRef(Name.data(), Name.size()));
Chris Lattner7a4e4642009-07-10 21:57:21 +0000105
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000106 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000107 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000108 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
109 return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000110
111
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000112 const X86MachineFunctionInfo *Info;
Chris Lattnerc08872e2009-07-15 04:55:56 +0000113
114 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000115 if (info_item == FunctionInfoMap.end()) {
116 // Calculate apropriate function info and populate map
117 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
118 Info = &FunctionInfoMap[F];
119 } else {
120 Info = &info_item->second;
121 }
Chris Lattnercf1ed752009-09-11 04:28:13 +0000122
123 if (Info->getDecorationStyle() == None) return;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000124 const FunctionType *FT = F->getFunctionType();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000125
Chris Lattnercf1ed752009-09-11 04:28:13 +0000126 // "Pure" variadic functions do not receive @0 suffix.
127 if (!FT->isVarArg() || FT->getNumParams() == 0 ||
128 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
129 raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
130
131 if (Info->getDecorationStyle() == FastCall) {
132 if (Name[0] == '_')
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000133 Name[0] = '@';
Chris Lattnercf1ed752009-09-11 04:28:13 +0000134 else
135 Name.insert(Name.begin(), '@');
136 }
137}
138
139/// DecorateCygMingName - Query FunctionInfoMap and use this information for
140/// various name decorations for Cygwin and MingW.
141void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
142 const GlobalValue *GV) {
143 SmallString<128> NameStr(Name.begin(), Name.end());
144 DecorateCygMingName(NameStr, GV);
145 Name.assign(NameStr.begin(), NameStr.end());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000146}
147
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000148void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling20c568f2009-06-30 22:38:32 +0000149 unsigned FnAlign = MF.getAlignment();
Evan Cheng2338c5c2006-02-07 08:38:37 +0000150 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000151
Chris Lattnerc08872e2009-07-15 04:55:56 +0000152 if (Subtarget->isTargetCygMing())
153 DecorateCygMingName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000154
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000155 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner865aaf02009-08-03 22:16:57 +0000156 EmitAlignment(FnAlign, F);
157
Evan Cheng2338c5c2006-02-07 08:38:37 +0000158 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000159 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000160 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000161 case Function::PrivateLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000162 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000163 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 case Function::ExternalLinkage:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000165 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 break;
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000167 case Function::LinkerPrivateLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000168 case Function::LinkOnceAnyLinkage:
169 case Function::LinkOnceODRLinkage:
170 case Function::WeakAnyLinkage:
171 case Function::WeakODRLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000172 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000173 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +0000174 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000175 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << "\n"
177 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000178 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000179 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000180 }
181 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000182 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000183
184 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000185
186 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000187 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000188 else if (Subtarget->isTargetCygMing()) {
189 O << "\t.def\t " << CurrentFnName
190 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000191 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000192 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
193 << ";\t.endef\n";
194 }
195
Dan Gohmancf20ac42009-08-13 01:36:44 +0000196 O << CurrentFnName << ':';
197 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000198 O.PadToColumn(MAI->getCommentColumn());
199 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000200 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
201 }
202 O << '\n';
203
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000204 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000205 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000206 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000207 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000208}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000210/// runOnMachineFunction - This uses the printMachineInstruction()
211/// method to print assembly for each instruction.
212///
213bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
214 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000215 this->MF = &MF;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000216 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000217
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000218 SetupMachineFunction(MF);
219 O << "\n\n";
220
221 // Populate function information map. Actually, We don't want to populate
222 // non-stdcall or non-fastcall functions' information right now.
223 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
224 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
225
226 // Print out constants referenced by the function
227 EmitConstantPool(MF.getConstantPool());
228
229 if (F->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000230 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000231
232 // Print the 'header' of function
233 emitFunctionHeader(MF);
234
235 // Emit pre-function debug and/or EH information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000236 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000237 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000238
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000239 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000240 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000241 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
242 I != E; ++I) {
243 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000244 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
245 // This is an entry block or a block that's only reachable via a
246 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
247 } else {
Chris Lattner70a54c02009-09-13 18:25:37 +0000248 EmitBasicBlockStart(I);
Nate Begemancdf38c42006-05-02 05:37:32 +0000249 O << '\n';
250 }
Bill Wendling824a7212008-01-26 09:03:52 +0000251 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
252 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000253 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000254 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000255 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000256 printMachineInstruction(II);
257 }
258 }
Evan Cheng67afece2006-08-28 22:14:16 +0000259
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000260 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
261 // If the function is empty, then we need to emit *something*. Otherwise,
262 // the function's label might be associated with something that it wasn't
263 // meant to be associated with. We emit a noop in this situation.
264 // We are assuming inline asms are code.
265 O << "\tnop\n";
266 }
267
Chris Lattner33adcfb2009-08-22 21:43:10 +0000268 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000269 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000270
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000271 // Emit post-function debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000272 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000273 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000274
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000275 // Print out jump tables referenced by the function.
276 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000277
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000278 // We didn't modify anything.
279 return false;
280}
281
Chris Lattnerf666c092009-07-13 21:53:19 +0000282/// printSymbolOperand - Print a raw symbol reference operand. This handles
283/// jump tables, constant pools, global address and external symbols, all of
284/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000285void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
286 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000287 default: llvm_unreachable("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000288 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000289 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000290 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000291 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000292 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000293 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000294 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000295 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000296 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000297 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000298 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000299
300 const char *Suffix = "";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000301 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
302 Suffix = "$stub";
303 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
304 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000305 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000306 Suffix = "$non_lazy_ptr";
307
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000308 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattnerc08872e2009-07-15 04:55:56 +0000309 if (Subtarget->isTargetCygMing())
310 DecorateCygMingName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000311
Chris Lattner35c89612009-07-09 05:42:07 +0000312 // Handle dllimport linkage.
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000313 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
314 Name = "__imp_" + Name;
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000315
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000316 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
Chris Lattner46091d72009-09-11 06:59:18 +0000317 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
318 SmallString<128> NameStr;
319 Mang->getNameWithPrefix(NameStr, GV, true);
320 NameStr += "$non_lazy_ptr";
321 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
322 MCSymbol *&StubSym = GVStubs[Sym];
323 if (StubSym == 0) {
324 NameStr.clear();
325 Mang->getNameWithPrefix(NameStr, GV, false);
326 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
327 }
328 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000329 SmallString<128> NameStr;
330 Mang->getNameWithPrefix(NameStr, GV, true);
331 NameStr += "$non_lazy_ptr";
332 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
333 MCSymbol *&StubSym = HiddenGVStubs[Sym];
334 if (StubSym == 0) {
335 NameStr.clear();
336 Mang->getNameWithPrefix(NameStr, GV, false);
337 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
338 }
Chris Lattner46091d72009-09-11 06:59:18 +0000339 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
340 SmallString<128> NameStr;
341 Mang->getNameWithPrefix(NameStr, GV, true);
342 NameStr += "$stub";
343 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
344 MCSymbol *&StubSym = FnStubs[Sym];
345 if (StubSym == 0) {
346 NameStr.clear();
347 Mang->getNameWithPrefix(NameStr, GV, false);
348 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
349 }
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000350 }
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000351
352 // If the name begins with a dollar-sign, enclose it in parens. We do this
353 // to avoid having it look like an integer immediate to the assembler.
354 if (Name[0] == '$')
355 O << '(' << Name << ')';
356 else
357 O << Name;
Chris Lattnere3723332009-06-21 02:22:53 +0000358
Chris Lattner35c89612009-07-09 05:42:07 +0000359 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000360 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000361 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000362 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf813d7d2009-07-15 03:01:23 +0000363 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000364 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000365 Name += "$stub";
Chris Lattner46091d72009-09-11 06:59:18 +0000366 MCSymbol *&StubSym = FnStubs[OutContext.GetOrCreateSymbol(Name)];
367 if (StubSym == 0) {
368 Name.erase(Name.end()-5, Name.end());
369 StubSym = OutContext.GetOrCreateSymbol(Name);
370 }
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000371 }
372
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000373 // If the name begins with a dollar-sign, enclose it in parens. We do this
374 // to avoid having it look like an integer immediate to the assembler.
375 if (Name[0] == '$')
376 O << '(' << Name << ')';
377 else
378 O << Name;
Chris Lattnerf7789c72009-06-27 05:46:24 +0000379 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000380 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000381 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000382
383 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000384 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000385 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000386 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000387 break;
388 case X86II::MO_DARWIN_NONLAZY:
Chris Lattner75cdf272009-07-09 06:59:17 +0000389 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000390 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000391 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000392 break;
393 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
394 O << " + [.-";
395 PrintPICBaseSymbol();
396 O << ']';
397 break;
398 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000399 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
400 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000401 O << '-';
402 PrintPICBaseSymbol();
403 break;
404 case X86II::MO_TLSGD: O << "@TLSGD"; break;
405 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
406 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
407 case X86II::MO_TPOFF: O << "@TPOFF"; break;
408 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
409 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
410 case X86II::MO_GOT: O << "@GOT"; break;
411 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000412 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000413 }
414}
415
Chris Lattnerf666c092009-07-13 21:53:19 +0000416/// print_pcrel_imm - This is used to print an immediate value that ends up
417/// being encoded as a pc-relative value. These print slightly differently, for
418/// example, a $ is not emitted.
419void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
420 const MachineOperand &MO = MI->getOperand(OpNo);
421 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000422 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000423 case MachineOperand::MO_Immediate:
424 O << MO.getImm();
425 return;
426 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000427 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Chris Lattnerf666c092009-07-13 21:53:19 +0000428 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000429 case MachineOperand::MO_GlobalAddress:
Chris Lattner62c5c502009-07-13 22:07:30 +0000430 case MachineOperand::MO_ExternalSymbol:
431 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000432 return;
433 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000434}
435
436
Chris Lattner174f8162009-07-13 21:41:08 +0000437void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
438 const char *Modifier) {
439 const MachineOperand &MO = MI->getOperand(OpNo);
440 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000441 default: llvm_unreachable("unknown operand type!");
Chris Lattner174f8162009-07-13 21:41:08 +0000442 case MachineOperand::MO_Register: {
443 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
444 "Virtual registers should not make it this far!");
445 O << '%';
446 unsigned Reg = MO.getReg();
447 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000448 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
450 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner174f8162009-07-13 21:41:08 +0000451 Reg = getX86SubSuperRegister(Reg, VT);
452 }
453 O << TRI->getAsmName(Reg);
454 return;
455 }
456
457 case MachineOperand::MO_Immediate:
458 O << '$' << MO.getImm();
459 return;
460
461 case MachineOperand::MO_JumpTableIndex:
462 case MachineOperand::MO_ConstantPoolIndex:
463 case MachineOperand::MO_GlobalAddress:
464 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000465 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000466 printSymbolOperand(MO);
467 break;
468 }
469 }
470}
471
Nate Begeman391c5d22005-11-30 18:54:35 +0000472void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000473 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000474 assert(value <= 7 && "Invalid ssecc argument!");
475 switch (value) {
476 case 0: O << "eq"; break;
477 case 1: O << "lt"; break;
478 case 2: O << "le"; break;
479 case 3: O << "unord"; break;
480 case 4: O << "neq"; break;
481 case 5: O << "nlt"; break;
482 case 6: O << "nle"; break;
483 case 7: O << "ord"; break;
484 }
485}
486
Rafael Espindola094fad32009-04-08 21:14:34 +0000487void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000488 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000489 const MachineOperand &BaseReg = MI->getOperand(Op);
490 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000491 const MachineOperand &DispSpec = MI->getOperand(Op+3);
492
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000493 // If we really don't want to print out (rip), don't.
494 bool HasBaseReg = BaseReg.getReg() != 0;
495 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
496 BaseReg.getReg() == X86::RIP)
497 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000498
499 // HasParenPart - True if we will print out the () part of the mem ref.
500 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
501
502 if (DispSpec.isImm()) {
503 int DispVal = DispSpec.getImm();
504 if (DispVal || !HasParenPart)
505 O << DispVal;
506 } else {
507 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
508 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000509 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000510 }
511
512 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000513 assert(IndexReg.getReg() != X86::ESP &&
514 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000515
Dan Gohmand19a53b2008-06-30 22:03:41 +0000516 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000517 if (HasBaseReg)
518 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000519
520 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000521 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000522 printOperand(MI, Op+2, Modifier);
523 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000524 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000525 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000526 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000527 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000528 }
529}
530
Rafael Espindola094fad32009-04-08 21:14:34 +0000531void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000532 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000533 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000534 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000535 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000536 printOperand(MI, Op+4, Modifier);
537 O << ':';
538 }
Chris Lattner18c59872009-06-27 04:16:01 +0000539 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000540}
541
Anton Korobeynikov91364382008-06-28 11:08:09 +0000542void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000543 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000544 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000545 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000546
547 // We don't need .set machinery if we have GOT-style relocations
548 if (Subtarget->isPICStyleGOT())
549 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000550
Chris Lattner33adcfb2009-08-22 21:43:10 +0000551 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000552 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +0000553
554 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
555
Evan Chenged2fc712007-11-09 19:11:23 +0000556 if (Subtarget->isPICStyleRIPRel())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000557 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000558 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000559 else {
560 O << '-';
561 PrintPICBaseSymbol();
562 O << '\n';
563 }
Evan Chengcc415862007-11-09 01:32:10 +0000564}
565
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000566
Evan Cheng7ccced62006-02-18 00:15:05 +0000567void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000568 PrintPICBaseSymbol();
569 O << '\n';
570 PrintPICBaseSymbol();
571 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000572}
573
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000574void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
575 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000576 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000577 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000578 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000579
580 O << JTEntryDirective << ' ';
581
Chris Lattnere2c92082009-07-10 21:00:45 +0000582 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000583 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000584 << '_' << uid << "_set_" << MBB->getNumber();
585 } else if (Subtarget->isPICStyleGOT()) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000586 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000587 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000588 } else
Chris Lattner325d3dc2009-09-13 17:14:04 +0000589 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000590}
591
Chris Lattner37710712009-06-15 04:42:32 +0000592bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000593 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000594 switch (Mode) {
595 default: return true; // Unknown mode.
596 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000598 break;
599 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000601 break;
602 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000604 break;
605 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000607 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000608 case 'q': // Print DImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000610 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000611 }
612
Evan Chengae270f62008-07-07 22:21:06 +0000613 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000614 return false;
615}
616
Evan Cheng3d48a902006-04-28 21:19:05 +0000617/// PrintAsmOperand - Print out an operand for an inline asm expression.
618///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000619bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000620 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000621 const char *ExtraCode) {
622 // Does this asm operand have a single letter operand modifier?
623 if (ExtraCode && ExtraCode[0]) {
624 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000625
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000626 const MachineOperand &MO = MI->getOperand(OpNo);
627
Evan Cheng3d48a902006-04-28 21:19:05 +0000628 switch (ExtraCode[0]) {
629 default: return true; // Unknown modifier.
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000630 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
631 if (MO.isImm()) {
632 O << MO.getImm();
633 return false;
Dale Johannesen231843e2009-08-19 23:44:01 +0000634 }
Dale Johannesenf6163dc2009-08-25 00:16:14 +0000635 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
636 printSymbolOperand(MO);
637 return false;
638 }
Dale Johannesen231843e2009-08-19 23:44:01 +0000639 if (MO.isReg()) {
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000640 O << '(';
641 printOperand(MI, OpNo);
642 O << ')';
643 return false;
644 }
645 return true;
646
Chris Lattnerb4828722007-01-25 02:53:24 +0000647 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000648 if (MO.isImm())
649 O << MO.getImm();
650 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
651 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000652 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000653 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000654 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000655
656 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000657 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000658 O << '*';
659 printOperand(MI, OpNo);
660 return false;
661 }
662 return true;
663
Evan Cheng62f27002006-04-28 23:11:40 +0000664 case 'b': // Print QImode register
665 case 'h': // Print QImode high register
666 case 'w': // Print HImode register
667 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000668 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000669 if (MO.isReg())
670 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000671 printOperand(MI, OpNo);
672 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000673
Dale Johannesen9fcff042009-07-07 23:28:22 +0000674 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000675 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000676 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000677
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000678 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000679 // Note: this is a temporary solution. It should be handled target
680 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000681 if (MO.isImm()) {
682 O << -MO.getImm();
683 return false;
684 }
685 O << '-';
686 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000687 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000688
Evan Cheng3d48a902006-04-28 21:19:05 +0000689 printOperand(MI, OpNo);
690 return false;
691}
692
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000693bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000694 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000695 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000696 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000697 if (ExtraCode && ExtraCode[0]) {
698 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000699
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000700 switch (ExtraCode[0]) {
701 default: return true; // Unknown modifier.
702 case 'b': // Print QImode register
703 case 'h': // Print QImode high register
704 case 'w': // Print HImode register
705 case 'k': // Print SImode register
706 case 'q': // Print SImode register
707 // These only apply to registers, ignore on mem.
708 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000709 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000710 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000711 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000712 }
713 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000714 printMemReference(MI, OpNo);
715 return false;
716}
717
Daniel Dunbarec2e4672009-08-31 19:14:05 +0000718
Chris Lattner9b60e042009-08-16 04:28:14 +0000719
Bill Wendlingac06d002009-02-06 21:45:08 +0000720/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
721/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000722///
723void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
724 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000725
Chris Lattner634cca32009-09-09 20:34:59 +0000726 processDebugLoc(MI->getDebugLoc());
727
Chris Lattner4e68a2a2009-09-12 20:01:36 +0000728 printInstructionThroughMCStreamer(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000729
730 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
731 EmitComments(*MI);
732 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000733}
734
Chris Lattner40bbebd2009-07-21 18:38:57 +0000735void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000736 const TargetData *TD = TM.getTargetData();
737
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000738 if (!GVar->hasInitializer())
739 return; // External global require no code
740
741 // Check to see if this is a special global used by LLVM, if so, emit it.
742 if (EmitSpecialLLVMGlobal(GVar)) {
743 if (Subtarget->isTargetDarwin() &&
744 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000745 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000746 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000747 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000748 O << ".reference .destructors_used\n";
749 }
750 return;
751 }
752
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000753 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000754 Constant *C = GVar->getInitializer();
755 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000756 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000757 unsigned Align = TD->getPreferredAlignmentLog(GVar);
758
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000759 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000760
761 if (Subtarget->isTargetELF())
762 O << "\t.type\t" << name << ",@object\n";
763
Chris Lattner26d054d2009-08-05 05:21:07 +0000764
765 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnera87dea42009-07-31 18:48:30 +0000766 const MCSection *TheSection =
Chris Lattner26d054d2009-08-05 05:21:07 +0000767 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000768 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000769
Chris Lattner3b24c012009-08-04 05:35:56 +0000770 // FIXME: get this stuff from section kind flags.
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000771 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000772 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000773 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000774 if (GVar->hasExternalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000775 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000776 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000777 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000778 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000779 return;
780 }
781 }
782
783 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000784 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000785 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000786
Chris Lattner33adcfb2009-08-22 21:43:10 +0000787 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000788 if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000789 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000790 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000791 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000792 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000793 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000794 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000795 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000796 O << name << ":";
797 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000798 O.PadToColumn(MAI->getCommentColumn());
799 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000800 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000801 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000802 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000803 EmitGlobalConstant(C);
804 return;
805 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000806 O << MAI->getCOMMDirective() << name << ',' << Size;
807 if (MAI->getCOMMDirectiveTakesAlignment())
808 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000809 }
810 } else {
811 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000812 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000813 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000814 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000815 O << MAI->getCOMMDirective() << name << ',' << Size;
816 if (MAI->getCOMMDirectiveTakesAlignment())
817 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000818 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000819 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000820 O.PadToColumn(MAI->getCommentColumn());
821 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000822 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000823 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000824 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000825 return;
826 }
827 }
828
829 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000830 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000831 case GlobalValue::LinkOnceAnyLinkage:
832 case GlobalValue::LinkOnceODRLinkage:
833 case GlobalValue::WeakAnyLinkage:
834 case GlobalValue::WeakODRLinkage:
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000835 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000836 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000837 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000838 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000839 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000840 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000841 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000842 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000843 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000844 }
845 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000846 case GlobalValue::DLLExportLinkage:
847 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848 // FIXME: appending linkage variables should go into a section of
849 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000850 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000851 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000852 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000853 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000854 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000855 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000857 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000858 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000859 }
860
861 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000862 O << name << ":";
863 if (VerboseAsm){
Chris Lattner33adcfb2009-08-22 21:43:10 +0000864 O.PadToColumn(MAI->getCommentColumn());
865 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000866 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000867 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000868 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000869
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000870 EmitGlobalConstant(C);
Dan Gohmancf20ac42009-08-13 01:36:44 +0000871
Chris Lattner33adcfb2009-08-22 21:43:10 +0000872 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmancf20ac42009-08-13 01:36:44 +0000873 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000874}
875
Chris Lattnerf4111632009-09-11 18:20:26 +0000876static int SortSymbolPair(const void *LHS, const void *RHS) {
877 MCSymbol *LHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)LHS)->first;
Chris Lattner6f8e4db2009-09-12 00:49:00 +0000878 MCSymbol *RHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)RHS)->first;
Chris Lattnerf4111632009-09-11 18:20:26 +0000879 return LHSS->getName().compare(RHSS->getName());
880}
881
882/// GetSortedStubs - Return the entries from a DenseMap in a deterministic
883/// sorted orer.
884static std::vector<std::pair<MCSymbol*, MCSymbol*> >
885GetSortedStubs(const DenseMap<MCSymbol*, MCSymbol*> &Map) {
886 assert(!Map.empty());
887 std::vector<std::pair<MCSymbol*, MCSymbol*> > List(Map.begin(), Map.end());
888 qsort(&List[0], List.size(), sizeof(List[0]), SortSymbolPair);
889 return List;
890}
891
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000892bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000893 // Print out module-level global variables here.
894 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000895 I != E; ++I) {
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000896 if (I->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000897 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000898 }
899
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000900 if (Subtarget->isTargetDarwin()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000901 // All darwin targets use mach-o.
902 TargetLoweringObjectFileMachO &TLOFMacho =
903 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner8f61f982009-06-24 18:24:09 +0000904
Chris Lattner381d4fe2009-06-24 18:17:00 +0000905 // Add the (possibly multiple) personalities to the set of global value
906 // stubs. Only referenced functions get into the Personalities list.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000907 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000908 const std::vector<Function*> &Personalities = MMI->getPersonalities();
909 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattner46091d72009-09-11 06:59:18 +0000910 if (Personalities[i] == 0)
911 continue;
912
913 SmallString<128> Name;
914 Mang->getNameWithPrefix(Name, Personalities[i], true /*private label*/);
915 Name += "$non_lazy_ptr";
916 MCSymbol *NLPName = OutContext.GetOrCreateSymbol(Name.str());
917
918 MCSymbol *&StubName = GVStubs[NLPName];
919 if (StubName != 0) continue;
920
921
922 Name.clear();
923 Mang->getNameWithPrefix(Name, Personalities[i], false);
924 StubName = OutContext.GetOrCreateSymbol(Name.str());
Evan Cheng77c8f762008-07-08 00:55:58 +0000925 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000926 }
927
Chris Lattner8f61f982009-06-24 18:24:09 +0000928 // Output stubs for dynamically-linked functions
929 if (!FnStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000930 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000931 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
932 MCSectionMachO::S_SYMBOL_STUBS |
933 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
934 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
935 5, SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000936 OutStreamer.SwitchSection(TheSection);
Chris Lattnerf4111632009-09-11 18:20:26 +0000937
938 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
939 = GetSortedStubs(FnStubs);
940 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
941 Stubs[i].first->print(O, MAI);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000942 O << ":\n" << "\t.indirect_symbol ";
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000943 // Get the MCSymbol without the $stub suffix.
Chris Lattnerf4111632009-09-11 18:20:26 +0000944 Stubs[i].second->print(O, MAI);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000945 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
946 }
Chris Lattner8f61f982009-06-24 18:24:09 +0000947 O << '\n';
948 }
949
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000950 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000951 if (!GVStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000952 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000953 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
954 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner11e96572009-08-03 21:53:27 +0000955 SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000956 OutStreamer.SwitchSection(TheSection);
Chris Lattnerf4111632009-09-11 18:20:26 +0000957
958 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
Chris Lattner24729e82009-09-11 18:33:44 +0000959 = GetSortedStubs(GVStubs);
Chris Lattnerf4111632009-09-11 18:20:26 +0000960 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
961 Stubs[i].first->print(O, MAI);
Chris Lattner46091d72009-09-11 06:59:18 +0000962 O << ":\n\t.indirect_symbol ";
Chris Lattnerf4111632009-09-11 18:20:26 +0000963 Stubs[i].second->print(O, MAI);
Chris Lattner46091d72009-09-11 06:59:18 +0000964 O << "\n\t.long\t0\n";
965 }
Chris Lattner8f61f982009-06-24 18:24:09 +0000966 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000967
Evan Chengae94e592008-12-05 01:06:39 +0000968 if (!HiddenGVStubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000969 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000970 EmitAlignment(2);
Chris Lattnerf4111632009-09-11 18:20:26 +0000971
972 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
Chris Lattner24729e82009-09-11 18:33:44 +0000973 = GetSortedStubs(HiddenGVStubs);
Chris Lattnerf4111632009-09-11 18:20:26 +0000974 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
975 Stubs[i].first->print(O, MAI);
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000976 O << ":\n" << MAI->getData32bitsDirective();
Chris Lattnerf4111632009-09-11 18:20:26 +0000977 Stubs[i].second->print(O, MAI);
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000978 O << '\n';
979 }
Evan Chengae94e592008-12-05 01:06:39 +0000980 }
981
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000982 // Funny Darwin hack: This flag tells the linker that no global symbols
983 // contain code that falls through to other global symbols (e.g. the obvious
984 // implementation of multiple entry points). If this doesn't occur, the
985 // linker can safely perform dead code stripping. Since LLVM never
986 // generates code that does this, it is always safe to set.
987 O << "\t.subsections_via_symbols\n";
988 } else if (Subtarget->isTargetCygMing()) {
989 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000990 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000991 i != e; ++i) {
992 O << "\t.def\t " << i->getKeyData()
993 << ";\t.scl\t" << COFF::C_EXT
994 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
995 << ";\t.endef\n";
996 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000997 }
Chris Lattner974469d2009-06-24 05:47:59 +0000998
Chris Lattner0a7befa2009-06-24 18:52:01 +0000999
1000 // Output linker support code for dllexported globals on windows.
Chris Lattner11e96572009-08-03 21:53:27 +00001001 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
1002 // dllexport symbols only exist on coff targets.
1003 TargetLoweringObjectFileCOFF &TLOFMacho =
1004 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
1005
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001006 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
1007 SectionKind::getMetadata()));
Chris Lattner0a7befa2009-06-24 18:52:01 +00001008
1009 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1010 e = DLLExportedGVs.end(); i != e; ++i)
1011 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattner0a7befa2009-06-24 18:52:01 +00001012
1013 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1014 e = DLLExportedFns.end();
1015 i != e; ++i)
1016 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1017 }
1018
Chris Lattner0a7befa2009-06-24 18:52:01 +00001019 // Do common shutdown.
Chris Lattner2b2954f2009-07-27 21:28:04 +00001020 return AsmPrinter::doFinalization(M);
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001021}
1022