blob: 4aef27e3303171d96046448ae79b266f7075f3ff [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"
Chris Lattnercae05cb2009-09-13 19:30:11 +000018#include "X86ATTInstPrinter.h"
Chris Lattner09abd1c2009-09-13 18:33:59 +000019#include "X86MCInstLower.h"
Cedric Venetd85f51a2008-08-24 12:30:46 +000020#include "X86.h"
21#include "X86COFF.h"
22#include "X86MachineFunctionInfo.h"
23#include "X86TargetMachine.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000024#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000025#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000026#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000027#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000028#include "llvm/Assembly/Writer.h"
Chris Lattner2a3c20b2009-09-11 06:36:33 +000029#include "llvm/MC/MCContext.h"
Chris Lattner522e9a02009-09-02 17:35:12 +000030#include "llvm/MC/MCSectionMachO.h"
Chris Lattner2a3c20b2009-09-11 06:36:33 +000031#include "llvm/MC/MCStreamer.h"
32#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000036#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000038#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000039#include "llvm/Target/TargetOptions.h"
Chris Lattnercf1ed752009-09-11 04:28:13 +000040#include "llvm/ADT/SmallString.h"
41#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000043
Chris Lattner95b2c7d2006-12-19 22:59:26 +000044STATISTIC(EmittedInsts, "Number of machine instrs printed");
45
Chris Lattnerab162992009-06-24 19:44:36 +000046//===----------------------------------------------------------------------===//
47// Primitive Helper Functions.
48//===----------------------------------------------------------------------===//
Chris Lattnerb5299dd2009-06-24 19:19:16 +000049
Chris Lattnercae05cb2009-09-13 19:30:11 +000050void X86ATTAsmPrinter::printMCInst(const MCInst *MI) {
Chris Lattnerc510f4c2009-09-13 20:15:16 +000051 X86ATTInstPrinter(O, MAI).printInstruction(MI);
Chris Lattnercae05cb2009-09-13 19:30:11 +000052}
53
Chris Lattnerb5299dd2009-06-24 19:19:16 +000054void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner09abd1c2009-09-13 18:33:59 +000055 // FIXME: Gross const cast hack.
56 X86ATTAsmPrinter *AP = const_cast<X86ATTAsmPrinter*>(this);
57 X86MCInstLower(OutContext, 0, *AP).GetPICBaseSymbol()->print(O, MAI);
Chris Lattner9b60e042009-08-16 04:28:14 +000058}
59
Anton Korobeynikov75b68822008-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 Patel05988662008-09-25 21:00:45 +000082 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000083 Ty = cast<PointerType>(Ty)->getElementType();
84
85 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000086 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-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
Chris Lattnerc08872e2009-07-15 04:55:56 +000094/// DecorateCygMingName - Query FunctionInfoMap and use this information for
95/// various name decorations for Cygwin and MingW.
Chris Lattnercf1ed752009-09-11 04:28:13 +000096void X86ATTAsmPrinter::DecorateCygMingName(SmallVectorImpl<char> &Name,
Chris Lattnerc08872e2009-07-15 04:55:56 +000097 const GlobalValue *GV) {
98 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
99
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000100 const Function *F = dyn_cast<Function>(GV);
101 if (!F) return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000102
Chris Lattner7a4e4642009-07-10 21:57:21 +0000103 // Save function name for later type emission.
Chris Lattnerc08872e2009-07-15 04:55:56 +0000104 if (F->isDeclaration())
Chris Lattnercf1ed752009-09-11 04:28:13 +0000105 CygMingStubs.insert(StringRef(Name.data(), Name.size()));
Chris Lattner7a4e4642009-07-10 21:57:21 +0000106
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000107 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000108 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000109 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
110 return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000111
112
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000113 const X86MachineFunctionInfo *Info;
Chris Lattnerc08872e2009-07-15 04:55:56 +0000114
115 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000116 if (info_item == FunctionInfoMap.end()) {
117 // Calculate apropriate function info and populate map
118 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
119 Info = &FunctionInfoMap[F];
120 } else {
121 Info = &info_item->second;
122 }
Chris Lattnercf1ed752009-09-11 04:28:13 +0000123
124 if (Info->getDecorationStyle() == None) return;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000125 const FunctionType *FT = F->getFunctionType();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000126
Chris Lattnercf1ed752009-09-11 04:28:13 +0000127 // "Pure" variadic functions do not receive @0 suffix.
128 if (!FT->isVarArg() || FT->getNumParams() == 0 ||
129 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
130 raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
131
132 if (Info->getDecorationStyle() == FastCall) {
133 if (Name[0] == '_')
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000134 Name[0] = '@';
Chris Lattnercf1ed752009-09-11 04:28:13 +0000135 else
136 Name.insert(Name.begin(), '@');
137 }
138}
139
140/// DecorateCygMingName - Query FunctionInfoMap and use this information for
141/// various name decorations for Cygwin and MingW.
142void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
143 const GlobalValue *GV) {
144 SmallString<128> NameStr(Name.begin(), Name.end());
145 DecorateCygMingName(NameStr, GV);
146 Name.assign(NameStr.begin(), NameStr.end());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000147}
148
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000149void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling20c568f2009-06-30 22:38:32 +0000150 unsigned FnAlign = MF.getAlignment();
Evan Cheng2338c5c2006-02-07 08:38:37 +0000151 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000152
Chris Lattnerc08872e2009-07-15 04:55:56 +0000153 if (Subtarget->isTargetCygMing())
154 DecorateCygMingName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000155
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000156 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner865aaf02009-08-03 22:16:57 +0000157 EmitAlignment(FnAlign, F);
158
Evan Cheng2338c5c2006-02-07 08:38:37 +0000159 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000160 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000161 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000162 case Function::PrivateLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000163 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000164 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000165 case Function::ExternalLinkage:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000166 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000167 break;
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000168 case Function::LinkerPrivateLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000169 case Function::LinkOnceAnyLinkage:
170 case Function::LinkOnceODRLinkage:
171 case Function::WeakAnyLinkage:
172 case Function::WeakODRLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000173 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000174 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +0000175 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000176 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << "\n"
178 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000179 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000180 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000181 }
182 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000183 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000184
185 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000186
187 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000188 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000189 else if (Subtarget->isTargetCygMing()) {
190 O << "\t.def\t " << CurrentFnName
191 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000192 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000193 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
194 << ";\t.endef\n";
195 }
196
Dan Gohmancf20ac42009-08-13 01:36:44 +0000197 O << CurrentFnName << ':';
198 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000199 O.PadToColumn(MAI->getCommentColumn());
200 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000201 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
202 }
203 O << '\n';
204
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000205 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000206 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000207 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000209}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000210
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000211/// runOnMachineFunction - This uses the printMachineInstruction()
212/// method to print assembly for each instruction.
213///
214bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
215 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000216 this->MF = &MF;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000217 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000218
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000219 SetupMachineFunction(MF);
220 O << "\n\n";
221
222 // Populate function information map. Actually, We don't want to populate
223 // non-stdcall or non-fastcall functions' information right now.
224 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
225 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
226
227 // Print out constants referenced by the function
228 EmitConstantPool(MF.getConstantPool());
229
230 if (F->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000231 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000232
233 // Print the 'header' of function
234 emitFunctionHeader(MF);
235
236 // Emit pre-function debug and/or EH information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000237 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000238 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000239
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000240 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000241 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000242 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
243 I != E; ++I) {
244 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000245 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
246 // This is an entry block or a block that's only reachable via a
247 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
248 } else {
Chris Lattner70a54c02009-09-13 18:25:37 +0000249 EmitBasicBlockStart(I);
Nate Begemancdf38c42006-05-02 05:37:32 +0000250 O << '\n';
251 }
Bill Wendling824a7212008-01-26 09:03:52 +0000252 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
253 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000254 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000255 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000256 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000257 printMachineInstruction(II);
258 }
259 }
Evan Cheng67afece2006-08-28 22:14:16 +0000260
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000261 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
262 // If the function is empty, then we need to emit *something*. Otherwise,
263 // the function's label might be associated with something that it wasn't
264 // meant to be associated with. We emit a noop in this situation.
265 // We are assuming inline asms are code.
266 O << "\tnop\n";
267 }
268
Chris Lattner33adcfb2009-08-22 21:43:10 +0000269 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000270 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000271
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000272 // Emit post-function debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000273 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000274 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000275
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000276 // Print out jump tables referenced by the function.
277 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000278
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000279 // We didn't modify anything.
280 return false;
281}
282
Chris Lattnerf666c092009-07-13 21:53:19 +0000283/// printSymbolOperand - Print a raw symbol reference operand. This handles
284/// jump tables, constant pools, global address and external symbols, all of
285/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000286void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
287 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000288 default: llvm_unreachable("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000289 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000290 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000291 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000292 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000293 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000294 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000295 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000296 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000297 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000298 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000299 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000300
301 const char *Suffix = "";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000302 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
303 Suffix = "$stub";
304 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
305 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000306 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000307 Suffix = "$non_lazy_ptr";
308
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000309 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattnerc08872e2009-07-15 04:55:56 +0000310 if (Subtarget->isTargetCygMing())
311 DecorateCygMingName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000312
Chris Lattner35c89612009-07-09 05:42:07 +0000313 // Handle dllimport linkage.
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000314 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
315 Name = "__imp_" + Name;
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000316
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000317 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
Chris Lattner46091d72009-09-11 06:59:18 +0000318 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
319 SmallString<128> NameStr;
320 Mang->getNameWithPrefix(NameStr, GV, true);
321 NameStr += "$non_lazy_ptr";
322 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
323 MCSymbol *&StubSym = GVStubs[Sym];
324 if (StubSym == 0) {
325 NameStr.clear();
326 Mang->getNameWithPrefix(NameStr, GV, false);
327 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
328 }
329 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000330 SmallString<128> NameStr;
331 Mang->getNameWithPrefix(NameStr, GV, true);
332 NameStr += "$non_lazy_ptr";
333 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
334 MCSymbol *&StubSym = HiddenGVStubs[Sym];
335 if (StubSym == 0) {
336 NameStr.clear();
337 Mang->getNameWithPrefix(NameStr, GV, false);
338 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
339 }
Chris Lattner46091d72009-09-11 06:59:18 +0000340 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
341 SmallString<128> NameStr;
342 Mang->getNameWithPrefix(NameStr, GV, true);
343 NameStr += "$stub";
344 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
345 MCSymbol *&StubSym = FnStubs[Sym];
346 if (StubSym == 0) {
347 NameStr.clear();
348 Mang->getNameWithPrefix(NameStr, GV, false);
349 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
350 }
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000351 }
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000352
353 // If the name begins with a dollar-sign, enclose it in parens. We do this
354 // to avoid having it look like an integer immediate to the assembler.
355 if (Name[0] == '$')
356 O << '(' << Name << ')';
357 else
358 O << Name;
Chris Lattnere3723332009-06-21 02:22:53 +0000359
Chris Lattner35c89612009-07-09 05:42:07 +0000360 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000361 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000362 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000363 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf813d7d2009-07-15 03:01:23 +0000364 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000365 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000366 Name += "$stub";
Chris Lattner46091d72009-09-11 06:59:18 +0000367 MCSymbol *&StubSym = FnStubs[OutContext.GetOrCreateSymbol(Name)];
368 if (StubSym == 0) {
369 Name.erase(Name.end()-5, Name.end());
370 StubSym = OutContext.GetOrCreateSymbol(Name);
371 }
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000372 }
373
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000374 // If the name begins with a dollar-sign, enclose it in parens. We do this
375 // to avoid having it look like an integer immediate to the assembler.
376 if (Name[0] == '$')
377 O << '(' << Name << ')';
378 else
379 O << Name;
Chris Lattnerf7789c72009-06-27 05:46:24 +0000380 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000381 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000382 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000383
384 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000385 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000386 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000387 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000388 break;
389 case X86II::MO_DARWIN_NONLAZY:
Chris Lattner75cdf272009-07-09 06:59:17 +0000390 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000391 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000392 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000393 break;
394 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
395 O << " + [.-";
396 PrintPICBaseSymbol();
397 O << ']';
398 break;
399 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000400 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
401 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000402 O << '-';
403 PrintPICBaseSymbol();
404 break;
405 case X86II::MO_TLSGD: O << "@TLSGD"; break;
406 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
407 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
408 case X86II::MO_TPOFF: O << "@TPOFF"; break;
409 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
410 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
411 case X86II::MO_GOT: O << "@GOT"; break;
412 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000413 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000414 }
415}
416
Chris Lattnerf666c092009-07-13 21:53:19 +0000417/// print_pcrel_imm - This is used to print an immediate value that ends up
418/// being encoded as a pc-relative value. These print slightly differently, for
419/// example, a $ is not emitted.
420void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
421 const MachineOperand &MO = MI->getOperand(OpNo);
422 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000423 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000424 case MachineOperand::MO_Immediate:
425 O << MO.getImm();
426 return;
427 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000428 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Chris Lattnerf666c092009-07-13 21:53:19 +0000429 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000430 case MachineOperand::MO_GlobalAddress:
Chris Lattner62c5c502009-07-13 22:07:30 +0000431 case MachineOperand::MO_ExternalSymbol:
432 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000433 return;
434 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000435}
436
437
Chris Lattner174f8162009-07-13 21:41:08 +0000438void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
439 const char *Modifier) {
440 const MachineOperand &MO = MI->getOperand(OpNo);
441 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000442 default: llvm_unreachable("unknown operand type!");
Chris Lattner174f8162009-07-13 21:41:08 +0000443 case MachineOperand::MO_Register: {
Chris Lattner174f8162009-07-13 21:41:08 +0000444 O << '%';
445 unsigned Reg = MO.getReg();
446 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000447 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
449 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner174f8162009-07-13 21:41:08 +0000450 Reg = getX86SubSuperRegister(Reg, VT);
451 }
Chris Lattner762ccea2009-09-13 20:31:40 +0000452 O << X86ATTInstPrinter::getRegisterName(Reg);
Chris Lattner174f8162009-07-13 21:41:08 +0000453 return;
454 }
455
456 case MachineOperand::MO_Immediate:
457 O << '$' << MO.getImm();
458 return;
459
460 case MachineOperand::MO_JumpTableIndex:
461 case MachineOperand::MO_ConstantPoolIndex:
462 case MachineOperand::MO_GlobalAddress:
463 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000464 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000465 printSymbolOperand(MO);
466 break;
467 }
468 }
469}
470
Nate Begeman391c5d22005-11-30 18:54:35 +0000471void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000472 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000473 assert(value <= 7 && "Invalid ssecc argument!");
474 switch (value) {
475 case 0: O << "eq"; break;
476 case 1: O << "lt"; break;
477 case 2: O << "le"; break;
478 case 3: O << "unord"; break;
479 case 4: O << "neq"; break;
480 case 5: O << "nlt"; break;
481 case 6: O << "nle"; break;
482 case 7: O << "ord"; break;
483 }
484}
485
Rafael Espindola094fad32009-04-08 21:14:34 +0000486void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000487 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000488 const MachineOperand &BaseReg = MI->getOperand(Op);
489 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000490 const MachineOperand &DispSpec = MI->getOperand(Op+3);
491
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000492 // If we really don't want to print out (rip), don't.
493 bool HasBaseReg = BaseReg.getReg() != 0;
494 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
495 BaseReg.getReg() == X86::RIP)
496 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000497
498 // HasParenPart - True if we will print out the () part of the mem ref.
499 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
500
501 if (DispSpec.isImm()) {
502 int DispVal = DispSpec.getImm();
503 if (DispVal || !HasParenPart)
504 O << DispVal;
505 } else {
506 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
507 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000508 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000509 }
510
511 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000512 assert(IndexReg.getReg() != X86::ESP &&
513 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000514
Dan Gohmand19a53b2008-06-30 22:03:41 +0000515 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000516 if (HasBaseReg)
517 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000518
519 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000520 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000521 printOperand(MI, Op+2, Modifier);
522 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000523 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000524 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000525 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000526 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000527 }
528}
529
Rafael Espindola094fad32009-04-08 21:14:34 +0000530void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000531 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000532 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000533 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000534 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000535 printOperand(MI, Op+4, Modifier);
536 O << ':';
537 }
Chris Lattner18c59872009-06-27 04:16:01 +0000538 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000539}
540
Anton Korobeynikov91364382008-06-28 11:08:09 +0000541void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000542 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000543 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000544 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000545
546 // We don't need .set machinery if we have GOT-style relocations
547 if (Subtarget->isPICStyleGOT())
548 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000549
Chris Lattner33adcfb2009-08-22 21:43:10 +0000550 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000551 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +0000552
553 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
554
Evan Chenged2fc712007-11-09 19:11:23 +0000555 if (Subtarget->isPICStyleRIPRel())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000556 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000557 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000558 else {
559 O << '-';
560 PrintPICBaseSymbol();
561 O << '\n';
562 }
Evan Chengcc415862007-11-09 01:32:10 +0000563}
564
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000565
Evan Cheng7ccced62006-02-18 00:15:05 +0000566void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000567 PrintPICBaseSymbol();
568 O << '\n';
569 PrintPICBaseSymbol();
570 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000571}
572
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000573void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
574 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000575 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000576 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000577 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000578
579 O << JTEntryDirective << ' ';
580
Chris Lattnere2c92082009-07-10 21:00:45 +0000581 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000582 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000583 << '_' << uid << "_set_" << MBB->getNumber();
584 } else if (Subtarget->isPICStyleGOT()) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000585 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000586 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000587 } else
Chris Lattner325d3dc2009-09-13 17:14:04 +0000588 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000589}
590
Chris Lattner37710712009-06-15 04:42:32 +0000591bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000592 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000593 switch (Mode) {
594 default: return true; // Unknown mode.
595 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000597 break;
598 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000600 break;
601 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000603 break;
604 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000606 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000607 case 'q': // Print DImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000609 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000610 }
611
Chris Lattner762ccea2009-09-13 20:31:40 +0000612 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000613 return false;
614}
615
Evan Cheng3d48a902006-04-28 21:19:05 +0000616/// PrintAsmOperand - Print out an operand for an inline asm expression.
617///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000618bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000619 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000620 const char *ExtraCode) {
621 // Does this asm operand have a single letter operand modifier?
622 if (ExtraCode && ExtraCode[0]) {
623 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000624
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000625 const MachineOperand &MO = MI->getOperand(OpNo);
626
Evan Cheng3d48a902006-04-28 21:19:05 +0000627 switch (ExtraCode[0]) {
628 default: return true; // Unknown modifier.
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000629 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
630 if (MO.isImm()) {
631 O << MO.getImm();
632 return false;
Dale Johannesen231843e2009-08-19 23:44:01 +0000633 }
Dale Johannesenf6163dc2009-08-25 00:16:14 +0000634 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
635 printSymbolOperand(MO);
636 return false;
637 }
Dale Johannesen231843e2009-08-19 23:44:01 +0000638 if (MO.isReg()) {
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000639 O << '(';
640 printOperand(MI, OpNo);
641 O << ')';
642 return false;
643 }
644 return true;
645
Chris Lattnerb4828722007-01-25 02:53:24 +0000646 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000647 if (MO.isImm())
648 O << MO.getImm();
649 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
650 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000651 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000652 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000653 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000654
655 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000656 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000657 O << '*';
658 printOperand(MI, OpNo);
659 return false;
660 }
661 return true;
662
Evan Cheng62f27002006-04-28 23:11:40 +0000663 case 'b': // Print QImode register
664 case 'h': // Print QImode high register
665 case 'w': // Print HImode register
666 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000667 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000668 if (MO.isReg())
669 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000670 printOperand(MI, OpNo);
671 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000672
Dale Johannesen9fcff042009-07-07 23:28:22 +0000673 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000674 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000675 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000676
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000677 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000678 // Note: this is a temporary solution. It should be handled target
679 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000680 if (MO.isImm()) {
681 O << -MO.getImm();
682 return false;
683 }
684 O << '-';
685 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000686 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000687
Evan Cheng3d48a902006-04-28 21:19:05 +0000688 printOperand(MI, OpNo);
689 return false;
690}
691
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000692bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000693 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000694 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000695 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000696 if (ExtraCode && ExtraCode[0]) {
697 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000698
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000699 switch (ExtraCode[0]) {
700 default: return true; // Unknown modifier.
701 case 'b': // Print QImode register
702 case 'h': // Print QImode high register
703 case 'w': // Print HImode register
704 case 'k': // Print SImode register
705 case 'q': // Print SImode register
706 // These only apply to registers, ignore on mem.
707 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000708 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000709 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000710 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000711 }
712 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000713 printMemReference(MI, OpNo);
714 return false;
715}
716
Daniel Dunbarec2e4672009-08-31 19:14:05 +0000717
Chris Lattner9b60e042009-08-16 04:28:14 +0000718
Bill Wendlingac06d002009-02-06 21:45:08 +0000719/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
720/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000721///
722void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
723 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000724
Chris Lattner634cca32009-09-09 20:34:59 +0000725 processDebugLoc(MI->getDebugLoc());
726
Chris Lattner4e68a2a2009-09-12 20:01:36 +0000727 printInstructionThroughMCStreamer(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000728
729 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
730 EmitComments(*MI);
731 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000732}
733
Chris Lattner40bbebd2009-07-21 18:38:57 +0000734void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000735 const TargetData *TD = TM.getTargetData();
736
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000737 if (!GVar->hasInitializer())
738 return; // External global require no code
739
740 // Check to see if this is a special global used by LLVM, if so, emit it.
741 if (EmitSpecialLLVMGlobal(GVar)) {
742 if (Subtarget->isTargetDarwin() &&
743 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000744 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000745 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000746 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000747 O << ".reference .destructors_used\n";
748 }
749 return;
750 }
751
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000752 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000753 Constant *C = GVar->getInitializer();
754 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000755 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000756 unsigned Align = TD->getPreferredAlignmentLog(GVar);
757
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000758 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000759
760 if (Subtarget->isTargetELF())
761 O << "\t.type\t" << name << ",@object\n";
762
Chris Lattner26d054d2009-08-05 05:21:07 +0000763
764 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnera87dea42009-07-31 18:48:30 +0000765 const MCSection *TheSection =
Chris Lattner26d054d2009-08-05 05:21:07 +0000766 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000767 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000768
Chris Lattner3b24c012009-08-04 05:35:56 +0000769 // FIXME: get this stuff from section kind flags.
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000770 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000771 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000772 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000773 if (GVar->hasExternalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000774 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000775 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000776 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000777 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000778 return;
779 }
780 }
781
782 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000783 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000784 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000785
Chris Lattner33adcfb2009-08-22 21:43:10 +0000786 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000787 if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000788 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000789 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000790 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000791 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000792 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000793 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000794 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000795 O << name << ":";
796 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000797 O.PadToColumn(MAI->getCommentColumn());
798 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000799 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000800 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000801 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000802 EmitGlobalConstant(C);
803 return;
804 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000805 O << MAI->getCOMMDirective() << name << ',' << Size;
806 if (MAI->getCOMMDirectiveTakesAlignment())
807 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000808 }
809 } else {
810 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000811 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000812 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000813 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000814 O << MAI->getCOMMDirective() << name << ',' << Size;
815 if (MAI->getCOMMDirectiveTakesAlignment())
816 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000817 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000818 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000819 O.PadToColumn(MAI->getCommentColumn());
820 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000821 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000822 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000823 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000824 return;
825 }
826 }
827
828 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000829 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000830 case GlobalValue::LinkOnceAnyLinkage:
831 case GlobalValue::LinkOnceODRLinkage:
832 case GlobalValue::WeakAnyLinkage:
833 case GlobalValue::WeakODRLinkage:
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000834 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000836 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000837 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000838 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000839 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000840 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000841 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000842 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000843 }
844 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000845 case GlobalValue::DLLExportLinkage:
846 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000847 // FIXME: appending linkage variables should go into a section of
848 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000849 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000853 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000854 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000855 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000856 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000857 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000858 }
859
860 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000861 O << name << ":";
862 if (VerboseAsm){
Chris Lattner33adcfb2009-08-22 21:43:10 +0000863 O.PadToColumn(MAI->getCommentColumn());
864 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000865 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000866 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000867 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000868
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000869 EmitGlobalConstant(C);
Dan Gohmancf20ac42009-08-13 01:36:44 +0000870
Chris Lattner33adcfb2009-08-22 21:43:10 +0000871 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmancf20ac42009-08-13 01:36:44 +0000872 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000873}
874
Chris Lattnerf4111632009-09-11 18:20:26 +0000875static int SortSymbolPair(const void *LHS, const void *RHS) {
876 MCSymbol *LHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)LHS)->first;
Chris Lattner6f8e4db2009-09-12 00:49:00 +0000877 MCSymbol *RHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)RHS)->first;
Chris Lattnerf4111632009-09-11 18:20:26 +0000878 return LHSS->getName().compare(RHSS->getName());
879}
880
881/// GetSortedStubs - Return the entries from a DenseMap in a deterministic
882/// sorted orer.
883static std::vector<std::pair<MCSymbol*, MCSymbol*> >
884GetSortedStubs(const DenseMap<MCSymbol*, MCSymbol*> &Map) {
885 assert(!Map.empty());
886 std::vector<std::pair<MCSymbol*, MCSymbol*> > List(Map.begin(), Map.end());
887 qsort(&List[0], List.size(), sizeof(List[0]), SortSymbolPair);
888 return List;
889}
890
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000891bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000892 // Print out module-level global variables here.
893 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000894 I != E; ++I) {
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000895 if (I->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000896 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000897 }
898
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000899 if (Subtarget->isTargetDarwin()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000900 // All darwin targets use mach-o.
901 TargetLoweringObjectFileMachO &TLOFMacho =
902 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner8f61f982009-06-24 18:24:09 +0000903
Chris Lattner381d4fe2009-06-24 18:17:00 +0000904 // Add the (possibly multiple) personalities to the set of global value
905 // stubs. Only referenced functions get into the Personalities list.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000906 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000907 const std::vector<Function*> &Personalities = MMI->getPersonalities();
908 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattner46091d72009-09-11 06:59:18 +0000909 if (Personalities[i] == 0)
910 continue;
911
912 SmallString<128> Name;
913 Mang->getNameWithPrefix(Name, Personalities[i], true /*private label*/);
914 Name += "$non_lazy_ptr";
915 MCSymbol *NLPName = OutContext.GetOrCreateSymbol(Name.str());
916
917 MCSymbol *&StubName = GVStubs[NLPName];
918 if (StubName != 0) continue;
919
920
921 Name.clear();
922 Mang->getNameWithPrefix(Name, Personalities[i], false);
923 StubName = OutContext.GetOrCreateSymbol(Name.str());
Evan Cheng77c8f762008-07-08 00:55:58 +0000924 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000925 }
926
Chris Lattner8f61f982009-06-24 18:24:09 +0000927 // Output stubs for dynamically-linked functions
928 if (!FnStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000929 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000930 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
931 MCSectionMachO::S_SYMBOL_STUBS |
932 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
933 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
934 5, SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000935 OutStreamer.SwitchSection(TheSection);
Chris Lattnerf4111632009-09-11 18:20:26 +0000936
937 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
938 = GetSortedStubs(FnStubs);
939 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
940 Stubs[i].first->print(O, MAI);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000941 O << ":\n" << "\t.indirect_symbol ";
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000942 // Get the MCSymbol without the $stub suffix.
Chris Lattnerf4111632009-09-11 18:20:26 +0000943 Stubs[i].second->print(O, MAI);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000944 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
945 }
Chris Lattner8f61f982009-06-24 18:24:09 +0000946 O << '\n';
947 }
948
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000949 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000950 if (!GVStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000951 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000952 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
953 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner11e96572009-08-03 21:53:27 +0000954 SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000955 OutStreamer.SwitchSection(TheSection);
Chris Lattnerf4111632009-09-11 18:20:26 +0000956
957 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
Chris Lattner24729e82009-09-11 18:33:44 +0000958 = GetSortedStubs(GVStubs);
Chris Lattnerf4111632009-09-11 18:20:26 +0000959 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
960 Stubs[i].first->print(O, MAI);
Chris Lattner46091d72009-09-11 06:59:18 +0000961 O << ":\n\t.indirect_symbol ";
Chris Lattnerf4111632009-09-11 18:20:26 +0000962 Stubs[i].second->print(O, MAI);
Chris Lattner46091d72009-09-11 06:59:18 +0000963 O << "\n\t.long\t0\n";
964 }
Chris Lattner8f61f982009-06-24 18:24:09 +0000965 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000966
Evan Chengae94e592008-12-05 01:06:39 +0000967 if (!HiddenGVStubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000968 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000969 EmitAlignment(2);
Chris Lattnerf4111632009-09-11 18:20:26 +0000970
971 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
Chris Lattner24729e82009-09-11 18:33:44 +0000972 = GetSortedStubs(HiddenGVStubs);
Chris Lattnerf4111632009-09-11 18:20:26 +0000973 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
974 Stubs[i].first->print(O, MAI);
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000975 O << ":\n" << MAI->getData32bitsDirective();
Chris Lattnerf4111632009-09-11 18:20:26 +0000976 Stubs[i].second->print(O, MAI);
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000977 O << '\n';
978 }
Evan Chengae94e592008-12-05 01:06:39 +0000979 }
980
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000981 // Funny Darwin hack: This flag tells the linker that no global symbols
982 // contain code that falls through to other global symbols (e.g. the obvious
983 // implementation of multiple entry points). If this doesn't occur, the
984 // linker can safely perform dead code stripping. Since LLVM never
985 // generates code that does this, it is always safe to set.
986 O << "\t.subsections_via_symbols\n";
987 } else if (Subtarget->isTargetCygMing()) {
988 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000989 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000990 i != e; ++i) {
991 O << "\t.def\t " << i->getKeyData()
992 << ";\t.scl\t" << COFF::C_EXT
993 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
994 << ";\t.endef\n";
995 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000996 }
Chris Lattner974469d2009-06-24 05:47:59 +0000997
Chris Lattner0a7befa2009-06-24 18:52:01 +0000998
999 // Output linker support code for dllexported globals on windows.
Chris Lattner11e96572009-08-03 21:53:27 +00001000 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
1001 // dllexport symbols only exist on coff targets.
1002 TargetLoweringObjectFileCOFF &TLOFMacho =
1003 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
1004
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001005 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
1006 SectionKind::getMetadata()));
Chris Lattner0a7befa2009-06-24 18:52:01 +00001007
1008 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1009 e = DLLExportedGVs.end(); i != e; ++i)
1010 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattner0a7befa2009-06-24 18:52:01 +00001011
1012 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1013 e = DLLExportedFns.end();
1014 i != e; ++i)
1015 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1016 }
1017
Chris Lattner0a7befa2009-06-24 18:52:01 +00001018 // Do common shutdown.
Chris Lattner2b2954f2009-07-27 21:28:04 +00001019 return AsmPrinter::doFinalization(M);
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001020}
1021