blob: 2d0ec2e3f79ebfe446c3143e76a7620bcbfb801c [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"
Chris Lattner475370b2009-06-19 00:47:33 +000032#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000037#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattnercf1ed752009-09-11 04:28:13 +000039#include "llvm/ADT/SmallString.h"
40#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000041using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042
Chris Lattner95b2c7d2006-12-19 22:59:26 +000043STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
Chris Lattner475370b2009-06-19 00:47:33 +000045static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
Chris Lattner4f8fb492009-09-11 17:07:27 +000046 cl::Hidden, cl::init(true));
Chris Lattner475370b2009-06-19 00:47:33 +000047
Chris Lattnerab162992009-06-24 19:44:36 +000048//===----------------------------------------------------------------------===//
49// Primitive Helper Functions.
50//===----------------------------------------------------------------------===//
Chris Lattnerb5299dd2009-06-24 19:19:16 +000051
52void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner9b60e042009-08-16 04:28:14 +000053 // FIXME: the actual label generated doesn't matter here! Just mangle in
54 // something unique (the function number) with Private prefix.
Evan Cheng071b9d52007-01-18 01:49:58 +000055 if (Subtarget->isTargetDarwin())
Chris Lattnerb5299dd2009-06-24 19:19:16 +000056 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattner9b60e042009-08-16 04:28:14 +000057 else {
58 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner8529d282009-07-08 23:09:14 +000059 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattner9b60e042009-08-16 04:28:14 +000060 }
61}
62
Anton Korobeynikov75b68822008-06-28 11:08:27 +000063static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
64 const TargetData *TD) {
65 X86MachineFunctionInfo Info;
66 uint64_t Size = 0;
67
68 switch (F->getCallingConv()) {
69 case CallingConv::X86_StdCall:
70 Info.setDecorationStyle(StdCall);
71 break;
72 case CallingConv::X86_FastCall:
73 Info.setDecorationStyle(FastCall);
74 break;
75 default:
76 return Info;
77 }
78
79 unsigned argNum = 1;
80 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
81 AI != AE; ++AI, ++argNum) {
82 const Type* Ty = AI->getType();
83
84 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000085 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000086 Ty = cast<PointerType>(Ty)->getElementType();
87
88 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000089 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov75b68822008-06-28 11:08:27 +000090 }
91
92 // We're not supporting tooooo huge arguments :)
93 Info.setBytesToPopOnReturn((unsigned int)Size);
94 return Info;
95}
96
Chris Lattnerc08872e2009-07-15 04:55:56 +000097/// DecorateCygMingName - Query FunctionInfoMap and use this information for
98/// various name decorations for Cygwin and MingW.
Chris Lattnercf1ed752009-09-11 04:28:13 +000099void X86ATTAsmPrinter::DecorateCygMingName(SmallVectorImpl<char> &Name,
Chris Lattnerc08872e2009-07-15 04:55:56 +0000100 const GlobalValue *GV) {
101 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
102
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000103 const Function *F = dyn_cast<Function>(GV);
104 if (!F) return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000105
Chris Lattner7a4e4642009-07-10 21:57:21 +0000106 // Save function name for later type emission.
Chris Lattnerc08872e2009-07-15 04:55:56 +0000107 if (F->isDeclaration())
Chris Lattnercf1ed752009-09-11 04:28:13 +0000108 CygMingStubs.insert(StringRef(Name.data(), Name.size()));
Chris Lattner7a4e4642009-07-10 21:57:21 +0000109
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000110 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000111 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000112 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
113 return;
Chris Lattnercf1ed752009-09-11 04:28:13 +0000114
115
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000116 const X86MachineFunctionInfo *Info;
Chris Lattnerc08872e2009-07-15 04:55:56 +0000117
118 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000119 if (info_item == FunctionInfoMap.end()) {
120 // Calculate apropriate function info and populate map
121 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
122 Info = &FunctionInfoMap[F];
123 } else {
124 Info = &info_item->second;
125 }
Chris Lattnercf1ed752009-09-11 04:28:13 +0000126
127 if (Info->getDecorationStyle() == None) return;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000128 const FunctionType *FT = F->getFunctionType();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000129
Chris Lattnercf1ed752009-09-11 04:28:13 +0000130 // "Pure" variadic functions do not receive @0 suffix.
131 if (!FT->isVarArg() || FT->getNumParams() == 0 ||
132 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
133 raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
134
135 if (Info->getDecorationStyle() == FastCall) {
136 if (Name[0] == '_')
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000137 Name[0] = '@';
Chris Lattnercf1ed752009-09-11 04:28:13 +0000138 else
139 Name.insert(Name.begin(), '@');
140 }
141}
142
143/// DecorateCygMingName - Query FunctionInfoMap and use this information for
144/// various name decorations for Cygwin and MingW.
145void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
146 const GlobalValue *GV) {
147 SmallString<128> NameStr(Name.begin(), Name.end());
148 DecorateCygMingName(NameStr, GV);
149 Name.assign(NameStr.begin(), NameStr.end());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000150}
151
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000152void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling20c568f2009-06-30 22:38:32 +0000153 unsigned FnAlign = MF.getAlignment();
Evan Cheng2338c5c2006-02-07 08:38:37 +0000154 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000155
Chris Lattnerc08872e2009-07-15 04:55:56 +0000156 if (Subtarget->isTargetCygMing())
157 DecorateCygMingName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000158
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000159 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner865aaf02009-08-03 22:16:57 +0000160 EmitAlignment(FnAlign, F);
161
Evan Cheng2338c5c2006-02-07 08:38:37 +0000162 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000163 default: llvm_unreachable("Unknown linkage type!");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000165 case Function::PrivateLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000167 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 case Function::ExternalLinkage:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000169 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000170 break;
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000171 case Function::LinkerPrivateLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000172 case Function::LinkOnceAnyLinkage:
173 case Function::LinkOnceODRLinkage:
174 case Function::WeakAnyLinkage:
175 case Function::WeakODRLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +0000178 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000182 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000184 }
185 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000186 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000187
188 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000189
190 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000191 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000192 else if (Subtarget->isTargetCygMing()) {
193 O << "\t.def\t " << CurrentFnName
194 << ";\t.scl\t" <<
Rafael Espindolabb46f522009-01-15 20:18:42 +0000195 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000196 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
197 << ";\t.endef\n";
198 }
199
Dan Gohmancf20ac42009-08-13 01:36:44 +0000200 O << CurrentFnName << ':';
201 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000202 O.PadToColumn(MAI->getCommentColumn());
203 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000204 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
205 }
206 O << '\n';
207
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000208 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000209 if (Subtarget->isTargetCygMing() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000210 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000212}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
Bill Wendlingac06d002009-02-06 21:45:08 +0000219 this->MF = &MF;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000220 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000221
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000222 SetupMachineFunction(MF);
223 O << "\n\n";
224
225 // Populate function information map. Actually, We don't want to populate
226 // non-stdcall or non-fastcall functions' information right now.
227 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
228 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
229
230 // Print out constants referenced by the function
231 EmitConstantPool(MF.getConstantPool());
232
233 if (F->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000234 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000235
236 // Print the 'header' of function
237 emitFunctionHeader(MF);
238
239 // Emit pre-function debug and/or EH information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000240 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000241 DW->BeginFunction(&MF);
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000242
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000244 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246 I != E; ++I) {
247 // Print a label for the basic block.
Dan Gohman968dc7a2009-03-31 18:39:13 +0000248 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
249 // This is an entry block or a block that's only reachable via a
250 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
251 } else {
Evan Chengf1c0ae92009-03-24 00:17:40 +0000252 printBasicBlockLabel(I, true, true, VerboseAsm);
Nate Begemancdf38c42006-05-02 05:37:32 +0000253 O << '\n';
254 }
Bill Wendling824a7212008-01-26 09:03:52 +0000255 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
256 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000257 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000258 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000259 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260 printMachineInstruction(II);
261 }
262 }
Evan Cheng67afece2006-08-28 22:14:16 +0000263
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000264 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
265 // If the function is empty, then we need to emit *something*. Otherwise,
266 // the function's label might be associated with something that it wasn't
267 // meant to be associated with. We emit a noop in this situation.
268 // We are assuming inline asms are code.
269 O << "\tnop\n";
270 }
271
Chris Lattner33adcfb2009-08-22 21:43:10 +0000272 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000273 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000275 // Emit post-function debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000276 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Pateleb3fc282009-01-08 23:40:34 +0000277 DW->EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000278
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000279 // Print out jump tables referenced by the function.
280 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000281
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000282 // We didn't modify anything.
283 return false;
284}
285
Chris Lattnerf666c092009-07-13 21:53:19 +0000286/// printSymbolOperand - Print a raw symbol reference operand. This handles
287/// jump tables, constant pools, global address and external symbols, all of
288/// which print to a label with various suffixes for relocation types etc.
Chris Lattner174f8162009-07-13 21:41:08 +0000289void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
290 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000291 default: llvm_unreachable("unknown symbol type!");
Chris Lattnere2959dc2009-07-13 22:28:21 +0000292 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000293 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000294 << MO.getIndex();
Chris Lattnerf7789c72009-06-27 05:46:24 +0000295 break;
Chris Lattnere2959dc2009-07-13 22:28:21 +0000296 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000297 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000298 << MO.getIndex();
Chris Lattner03a597f2009-06-26 20:00:05 +0000299 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000300 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000301 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000302 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000303
304 const char *Suffix = "";
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000305 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
306 Suffix = "$stub";
307 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
308 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000309 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000310 Suffix = "$non_lazy_ptr";
311
Chris Lattnerb7b179e2009-07-15 01:53:36 +0000312 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattnerc08872e2009-07-15 04:55:56 +0000313 if (Subtarget->isTargetCygMing())
314 DecorateCygMingName(Name, GV);
Chris Lattner174f8162009-07-13 21:41:08 +0000315
Chris Lattner35c89612009-07-09 05:42:07 +0000316 // Handle dllimport linkage.
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000317 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
318 Name = "__imp_" + Name;
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000319
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000320 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
Chris Lattner46091d72009-09-11 06:59:18 +0000321 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
322 SmallString<128> NameStr;
323 Mang->getNameWithPrefix(NameStr, GV, true);
324 NameStr += "$non_lazy_ptr";
325 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
326 MCSymbol *&StubSym = GVStubs[Sym];
327 if (StubSym == 0) {
328 NameStr.clear();
329 Mang->getNameWithPrefix(NameStr, GV, false);
330 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
331 }
332 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000333 SmallString<128> NameStr;
334 Mang->getNameWithPrefix(NameStr, GV, true);
335 NameStr += "$non_lazy_ptr";
336 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
337 MCSymbol *&StubSym = HiddenGVStubs[Sym];
338 if (StubSym == 0) {
339 NameStr.clear();
340 Mang->getNameWithPrefix(NameStr, GV, false);
341 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
342 }
Chris Lattner46091d72009-09-11 06:59:18 +0000343 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
344 SmallString<128> NameStr;
345 Mang->getNameWithPrefix(NameStr, GV, true);
346 NameStr += "$stub";
347 MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
348 MCSymbol *&StubSym = FnStubs[Sym];
349 if (StubSym == 0) {
350 NameStr.clear();
351 Mang->getNameWithPrefix(NameStr, GV, false);
352 StubSym = OutContext.GetOrCreateSymbol(NameStr.str());
353 }
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000354 }
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000355
356 // If the name begins with a dollar-sign, enclose it in parens. We do this
357 // to avoid having it look like an integer immediate to the assembler.
358 if (Name[0] == '$')
359 O << '(' << Name << ')';
360 else
361 O << Name;
Chris Lattnere3723332009-06-21 02:22:53 +0000362
Chris Lattner35c89612009-07-09 05:42:07 +0000363 printOffset(MO.getOffset());
Chris Lattnerf7789c72009-06-27 05:46:24 +0000364 break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000365 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000366 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf813d7d2009-07-15 03:01:23 +0000367 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000368 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000369 Name += "$stub";
Chris Lattner46091d72009-09-11 06:59:18 +0000370 MCSymbol *&StubSym = FnStubs[OutContext.GetOrCreateSymbol(Name)];
371 if (StubSym == 0) {
372 Name.erase(Name.end()-5, Name.end());
373 StubSym = OutContext.GetOrCreateSymbol(Name);
374 }
Daniel Dunbar41ffe6c2009-07-14 15:57:55 +0000375 }
376
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000377 // If the name begins with a dollar-sign, enclose it in parens. We do this
378 // to avoid having it look like an integer immediate to the assembler.
379 if (Name[0] == '$')
380 O << '(' << Name << ')';
381 else
382 O << Name;
Chris Lattnerf7789c72009-06-27 05:46:24 +0000383 break;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000384 }
Chris Lattnere2959dc2009-07-13 22:28:21 +0000385 }
Chris Lattnerf7789c72009-06-27 05:46:24 +0000386
387 switch (MO.getTargetFlags()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000388 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000389 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000390 case X86II::MO_NO_FLAG: // No flag.
Chris Lattner75cdf272009-07-09 06:59:17 +0000391 break;
392 case X86II::MO_DARWIN_NONLAZY:
Chris Lattner75cdf272009-07-09 06:59:17 +0000393 case X86II::MO_DLLIMPORT:
Chris Lattner62c5c502009-07-13 22:07:30 +0000394 case X86II::MO_DARWIN_STUB:
Chris Lattner75cdf272009-07-09 06:59:17 +0000395 // These affect the name of the symbol, not any suffix.
Chris Lattnerf7789c72009-06-27 05:46:24 +0000396 break;
397 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
398 O << " + [.-";
399 PrintPICBaseSymbol();
400 O << ']';
401 break;
402 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattner75cdf272009-07-09 06:59:17 +0000403 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
404 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattnerf7789c72009-06-27 05:46:24 +0000405 O << '-';
406 PrintPICBaseSymbol();
407 break;
408 case X86II::MO_TLSGD: O << "@TLSGD"; break;
409 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
410 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
411 case X86II::MO_TPOFF: O << "@TPOFF"; break;
412 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
413 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
414 case X86II::MO_GOT: O << "@GOT"; break;
415 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattner62c5c502009-07-13 22:07:30 +0000416 case X86II::MO_PLT: O << "@PLT"; break;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000417 }
418}
419
Chris Lattnerf666c092009-07-13 21:53:19 +0000420/// print_pcrel_imm - This is used to print an immediate value that ends up
421/// being encoded as a pc-relative value. These print slightly differently, for
422/// example, a $ is not emitted.
423void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
424 const MachineOperand &MO = MI->getOperand(OpNo);
425 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000426 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerf666c092009-07-13 21:53:19 +0000427 case MachineOperand::MO_Immediate:
428 O << MO.getImm();
429 return;
430 case MachineOperand::MO_MachineBasicBlock:
Dan Gohmancf20ac42009-08-13 01:36:44 +0000431 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerf666c092009-07-13 21:53:19 +0000432 return;
Chris Lattner62c5c502009-07-13 22:07:30 +0000433 case MachineOperand::MO_GlobalAddress:
Chris Lattner62c5c502009-07-13 22:07:30 +0000434 case MachineOperand::MO_ExternalSymbol:
435 printSymbolOperand(MO);
Chris Lattnerf666c092009-07-13 21:53:19 +0000436 return;
437 }
Chris Lattnerf666c092009-07-13 21:53:19 +0000438}
439
440
Chris Lattner174f8162009-07-13 21:41:08 +0000441
442void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
443 const char *Modifier) {
444 const MachineOperand &MO = MI->getOperand(OpNo);
445 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000446 default: llvm_unreachable("unknown operand type!");
Chris Lattner174f8162009-07-13 21:41:08 +0000447 case MachineOperand::MO_Register: {
448 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
449 "Virtual registers should not make it this far!");
450 O << '%';
451 unsigned Reg = MO.getReg();
452 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000453 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
455 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner174f8162009-07-13 21:41:08 +0000456 Reg = getX86SubSuperRegister(Reg, VT);
457 }
458 O << TRI->getAsmName(Reg);
459 return;
460 }
461
462 case MachineOperand::MO_Immediate:
463 O << '$' << MO.getImm();
464 return;
465
466 case MachineOperand::MO_JumpTableIndex:
467 case MachineOperand::MO_ConstantPoolIndex:
468 case MachineOperand::MO_GlobalAddress:
469 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000470 O << '$';
Chris Lattner174f8162009-07-13 21:41:08 +0000471 printSymbolOperand(MO);
472 break;
473 }
474 }
475}
476
Nate Begeman391c5d22005-11-30 18:54:35 +0000477void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000478 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000479 assert(value <= 7 && "Invalid ssecc argument!");
480 switch (value) {
481 case 0: O << "eq"; break;
482 case 1: O << "lt"; break;
483 case 2: O << "le"; break;
484 case 3: O << "unord"; break;
485 case 4: O << "neq"; break;
486 case 5: O << "nlt"; break;
487 case 6: O << "nle"; break;
488 case 7: O << "ord"; break;
489 }
490}
491
Rafael Espindola094fad32009-04-08 21:14:34 +0000492void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000493 const char *Modifier) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000494 const MachineOperand &BaseReg = MI->getOperand(Op);
495 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000496 const MachineOperand &DispSpec = MI->getOperand(Op+3);
497
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000498 // If we really don't want to print out (rip), don't.
499 bool HasBaseReg = BaseReg.getReg() != 0;
500 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
501 BaseReg.getReg() == X86::RIP)
502 HasBaseReg = false;
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000503
504 // HasParenPart - True if we will print out the () part of the mem ref.
505 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
506
507 if (DispSpec.isImm()) {
508 int DispVal = DispSpec.getImm();
509 if (DispVal || !HasParenPart)
510 O << DispVal;
511 } else {
512 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
513 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000514 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerb172b0b2009-07-09 00:32:12 +0000515 }
516
517 if (HasParenPart) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000518 assert(IndexReg.getReg() != X86::ESP &&
519 "X86 doesn't allow scaling by ESP");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000520
Dan Gohmand19a53b2008-06-30 22:03:41 +0000521 O << '(';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000522 if (HasBaseReg)
523 printOperand(MI, Op, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000524
525 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000526 O << ',';
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000527 printOperand(MI, Op+2, Modifier);
528 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000529 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000530 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000531 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000532 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000533 }
534}
535
Rafael Espindola094fad32009-04-08 21:14:34 +0000536void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner18c59872009-06-27 04:16:01 +0000537 const char *Modifier) {
Rafael Espindola094fad32009-04-08 21:14:34 +0000538 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000539 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindola094fad32009-04-08 21:14:34 +0000540 if (Segment.getReg()) {
Chris Lattner9f44b3a2009-07-09 00:27:29 +0000541 printOperand(MI, Op+4, Modifier);
542 O << ':';
543 }
Chris Lattner18c59872009-06-27 04:16:01 +0000544 printLeaMemReference(MI, Op, Modifier);
Rafael Espindola094fad32009-04-08 21:14:34 +0000545}
546
Anton Korobeynikov91364382008-06-28 11:08:09 +0000547void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000548 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000549 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000550 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000551
552 // We don't need .set machinery if we have GOT-style relocations
553 if (Subtarget->isPICStyleGOT())
554 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000555
Chris Lattner33adcfb2009-08-22 21:43:10 +0000556 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000557 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000558 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000559 if (Subtarget->isPICStyleRIPRel())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000560 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000561 << '_' << uid << '\n';
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000562 else {
563 O << '-';
564 PrintPICBaseSymbol();
565 O << '\n';
566 }
Evan Chengcc415862007-11-09 01:32:10 +0000567}
568
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000569
Evan Cheng7ccced62006-02-18 00:15:05 +0000570void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb5299dd2009-06-24 19:19:16 +0000571 PrintPICBaseSymbol();
572 O << '\n';
573 PrintPICBaseSymbol();
574 O << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000575}
576
Evan Cheng62f27002006-04-28 23:11:40 +0000577
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000578void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
579 const MachineBasicBlock *MBB,
Chris Lattner7a4e4642009-07-10 21:57:21 +0000580 unsigned uid) const {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000581 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000582 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000583
584 O << JTEntryDirective << ' ';
585
Chris Lattnere2c92082009-07-10 21:00:45 +0000586 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000587 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3b67e9b2009-07-10 20:47:30 +0000588 << '_' << uid << "_set_" << MBB->getNumber();
589 } else if (Subtarget->isPICStyleGOT()) {
590 printBasicBlockLabel(MBB, false, false, false);
591 O << "@GOTOFF";
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000592 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000593 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000594}
595
Chris Lattner37710712009-06-15 04:42:32 +0000596bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000597 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000598 switch (Mode) {
599 default: return true; // Unknown mode.
600 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000602 break;
603 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000605 break;
606 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000608 break;
609 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000611 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000612 case 'q': // Print DImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000614 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000615 }
616
Evan Chengae270f62008-07-07 22:21:06 +0000617 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000618 return false;
619}
620
Evan Cheng3d48a902006-04-28 21:19:05 +0000621/// PrintAsmOperand - Print out an operand for an inline asm expression.
622///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000623bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000624 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000625 const char *ExtraCode) {
626 // Does this asm operand have a single letter operand modifier?
627 if (ExtraCode && ExtraCode[0]) {
628 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000629
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000630 const MachineOperand &MO = MI->getOperand(OpNo);
631
Evan Cheng3d48a902006-04-28 21:19:05 +0000632 switch (ExtraCode[0]) {
633 default: return true; // Unknown modifier.
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000634 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
635 if (MO.isImm()) {
636 O << MO.getImm();
637 return false;
Dale Johannesen231843e2009-08-19 23:44:01 +0000638 }
Dale Johannesenf6163dc2009-08-25 00:16:14 +0000639 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
640 printSymbolOperand(MO);
641 return false;
642 }
Dale Johannesen231843e2009-08-19 23:44:01 +0000643 if (MO.isReg()) {
Dale Johannesen7bcec7e2009-08-19 22:44:41 +0000644 O << '(';
645 printOperand(MI, OpNo);
646 O << ')';
647 return false;
648 }
649 return true;
650
Chris Lattnerb4828722007-01-25 02:53:24 +0000651 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000652 if (MO.isImm())
653 O << MO.getImm();
654 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
655 printSymbolOperand(MO);
Chris Lattner174f8162009-07-13 21:41:08 +0000656 else
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000657 printOperand(MI, OpNo);
Chris Lattner0d924992006-10-31 20:12:30 +0000658 return false;
Dale Johannesen39f59d82009-07-09 20:06:27 +0000659
660 case 'A': // Print '*' before a register (it must be a register)
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000661 if (MO.isReg()) {
Dale Johannesen39f59d82009-07-09 20:06:27 +0000662 O << '*';
663 printOperand(MI, OpNo);
664 return false;
665 }
666 return true;
667
Evan Cheng62f27002006-04-28 23:11:40 +0000668 case 'b': // Print QImode register
669 case 'h': // Print QImode high register
670 case 'w': // Print HImode register
671 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000672 case 'q': // Print DImode register
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000673 if (MO.isReg())
674 return printAsmMRegister(MO, ExtraCode[0]);
Chris Lattner14393522007-03-25 02:01:03 +0000675 printOperand(MI, OpNo);
676 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000677
Dale Johannesen9fcff042009-07-07 23:28:22 +0000678 case 'P': // This is the operand of a call, treat specially.
Chris Lattner44f7bbd2009-07-09 00:39:19 +0000679 print_pcrel_imm(MI, OpNo);
Chris Lattner7cd5e072007-03-25 01:44:57 +0000680 return false;
Evan Cheng2c2fb822009-06-26 22:00:19 +0000681
Chris Lattner92aa0bb2009-07-13 21:48:33 +0000682 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000683 // Note: this is a temporary solution. It should be handled target
684 // independently as part of the 'MC' work.
Evan Cheng2c2fb822009-06-26 22:00:19 +0000685 if (MO.isImm()) {
686 O << -MO.getImm();
687 return false;
688 }
689 O << '-';
690 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000691 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000692
Evan Cheng3d48a902006-04-28 21:19:05 +0000693 printOperand(MI, OpNo);
694 return false;
695}
696
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000697bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000698 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000699 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000700 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000701 if (ExtraCode && ExtraCode[0]) {
702 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000703
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000704 switch (ExtraCode[0]) {
705 default: return true; // Unknown modifier.
706 case 'b': // Print QImode register
707 case 'h': // Print QImode high register
708 case 'w': // Print HImode register
709 case 'k': // Print SImode register
710 case 'q': // Print SImode register
711 // These only apply to registers, ignore on mem.
712 break;
Chris Lattner91387de2009-01-23 22:33:40 +0000713 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner18c59872009-06-27 04:16:01 +0000714 printMemReference(MI, OpNo, "no-rip");
Chris Lattner91387de2009-01-23 22:33:40 +0000715 return false;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000716 }
717 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000718 printMemReference(MI, OpNo);
719 return false;
720}
721
Daniel Dunbarec2e4672009-08-31 19:14:05 +0000722
Chris Lattner9b60e042009-08-16 04:28:14 +0000723
Bill Wendlingac06d002009-02-06 21:45:08 +0000724/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
725/// AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000726///
727void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
728 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000729
Chris Lattner634cca32009-09-09 20:34:59 +0000730 processDebugLoc(MI->getDebugLoc());
731
Chris Lattner522e9a02009-09-02 17:35:12 +0000732 // Call the autogenerated instruction printer routines.
733 if (NewAsmPrinter)
734 printInstructionThroughMCStreamer(MI);
735 else
Chris Lattner9b60e042009-08-16 04:28:14 +0000736 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000737
738 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
739 EmitComments(*MI);
740 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000741}
742
Chris Lattner40bbebd2009-07-21 18:38:57 +0000743void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000744 const TargetData *TD = TM.getTargetData();
745
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000746 if (!GVar->hasInitializer())
747 return; // External global require no code
748
749 // Check to see if this is a special global used by LLVM, if so, emit it.
750 if (EmitSpecialLLVMGlobal(GVar)) {
751 if (Subtarget->isTargetDarwin() &&
752 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000753 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000754 O << ".reference .constructors_used\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +0000755 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000756 O << ".reference .destructors_used\n";
757 }
758 return;
759 }
760
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000761 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000762 Constant *C = GVar->getInitializer();
763 const Type *Type = C->getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000764 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000765 unsigned Align = TD->getPreferredAlignmentLog(GVar);
766
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000767 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000768
769 if (Subtarget->isTargetELF())
770 O << "\t.type\t" << name << ",@object\n";
771
Chris Lattner26d054d2009-08-05 05:21:07 +0000772
773 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnera87dea42009-07-31 18:48:30 +0000774 const MCSection *TheSection =
Chris Lattner26d054d2009-08-05 05:21:07 +0000775 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000776 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000777
Chris Lattner3b24c012009-08-04 05:35:56 +0000778 // FIXME: get this stuff from section kind flags.
Evan Chengcaa0c2c2009-02-18 02:19:52 +0000779 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattnerc440cc72009-07-24 04:08:17 +0000780 // Don't put things that should go in the cstring section into "comm".
Chris Lattner5fe575f2009-07-27 05:32:16 +0000781 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000782 if (GVar->hasExternalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000783 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000784 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000785 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000786 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000787 return;
788 }
789 }
790
791 if (!GVar->isThreadLocal() &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000792 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000793 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000794
Chris Lattner33adcfb2009-08-22 21:43:10 +0000795 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000796 if (GVar->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000797 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000798 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000799 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000800 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000801 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000802 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000803 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000804 O << name << ":";
805 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000806 O.PadToColumn(MAI->getCommentColumn());
807 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000808 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000809 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000810 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000811 EmitGlobalConstant(C);
812 return;
813 } else {
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 }
818 } else {
819 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000820 if (GVar->hasLocalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000821 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000822 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000823 O << MAI->getCOMMDirective() << name << ',' << Size;
824 if (MAI->getCOMMDirectiveTakesAlignment())
825 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000826 }
Evan Chengf1c0ae92009-03-24 00:17:40 +0000827 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000828 O.PadToColumn(MAI->getCommentColumn());
829 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000830 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000831 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000832 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 return;
834 }
835 }
836
837 switch (GVar->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000838 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000839 case GlobalValue::LinkOnceAnyLinkage:
840 case GlobalValue::LinkOnceODRLinkage:
841 case GlobalValue::WeakAnyLinkage:
842 case GlobalValue::WeakODRLinkage:
Dale Johannesenf991ecf2009-08-13 00:28:52 +0000843 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000844 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000845 O << "\t.globl " << name << '\n'
Chris Lattner33adcfb2009-08-22 21:43:10 +0000846 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000847 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000849 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852 }
853 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000854 case GlobalValue::DLLExportLinkage:
855 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 // FIXME: appending linkage variables should go into a section of
857 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000858 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000859 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000860 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000861 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000862 case GlobalValue::PrivateLinkage:
Evan Cheng15621a22008-08-08 06:43:59 +0000863 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000864 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000865 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000866 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000867 }
868
869 EmitAlignment(Align, GVar);
Evan Chengf1c0ae92009-03-24 00:17:40 +0000870 O << name << ":";
871 if (VerboseAsm){
Chris Lattner33adcfb2009-08-22 21:43:10 +0000872 O.PadToColumn(MAI->getCommentColumn());
873 O << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000874 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Chengf1c0ae92009-03-24 00:17:40 +0000875 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000876 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000877
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000878 EmitGlobalConstant(C);
Dan Gohmancf20ac42009-08-13 01:36:44 +0000879
Chris Lattner33adcfb2009-08-22 21:43:10 +0000880 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohmancf20ac42009-08-13 01:36:44 +0000881 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000882}
883
Chris Lattnerf4111632009-09-11 18:20:26 +0000884static int SortSymbolPair(const void *LHS, const void *RHS) {
885 MCSymbol *LHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)LHS)->first;
886 MCSymbol *RHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)LHS)->first;
887 return LHSS->getName().compare(RHSS->getName());
888}
889
890/// GetSortedStubs - Return the entries from a DenseMap in a deterministic
891/// sorted orer.
892static std::vector<std::pair<MCSymbol*, MCSymbol*> >
893GetSortedStubs(const DenseMap<MCSymbol*, MCSymbol*> &Map) {
894 assert(!Map.empty());
895 std::vector<std::pair<MCSymbol*, MCSymbol*> > List(Map.begin(), Map.end());
896 qsort(&List[0], List.size(), sizeof(List[0]), SortSymbolPair);
897 return List;
898}
899
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000900bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000901 // Print out module-level global variables here.
902 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000903 I != E; ++I) {
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000904 if (I->hasDLLExportLinkage())
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000905 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikovb5bd0262009-02-21 11:53:32 +0000906 }
907
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000908 if (Subtarget->isTargetDarwin()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000909 // All darwin targets use mach-o.
910 TargetLoweringObjectFileMachO &TLOFMacho =
911 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner8f61f982009-06-24 18:24:09 +0000912
Chris Lattner381d4fe2009-06-24 18:17:00 +0000913 // Add the (possibly multiple) personalities to the set of global value
914 // stubs. Only referenced functions get into the Personalities list.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000915 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner381d4fe2009-06-24 18:17:00 +0000916 const std::vector<Function*> &Personalities = MMI->getPersonalities();
917 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattner46091d72009-09-11 06:59:18 +0000918 if (Personalities[i] == 0)
919 continue;
920
921 SmallString<128> Name;
922 Mang->getNameWithPrefix(Name, Personalities[i], true /*private label*/);
923 Name += "$non_lazy_ptr";
924 MCSymbol *NLPName = OutContext.GetOrCreateSymbol(Name.str());
925
926 MCSymbol *&StubName = GVStubs[NLPName];
927 if (StubName != 0) continue;
928
929
930 Name.clear();
931 Mang->getNameWithPrefix(Name, Personalities[i], false);
932 StubName = OutContext.GetOrCreateSymbol(Name.str());
Evan Cheng77c8f762008-07-08 00:55:58 +0000933 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000934 }
935
Chris Lattner8f61f982009-06-24 18:24:09 +0000936 // Output stubs for dynamically-linked functions
937 if (!FnStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000938 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000939 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
940 MCSectionMachO::S_SYMBOL_STUBS |
941 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
942 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
943 5, SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000944 OutStreamer.SwitchSection(TheSection);
Chris Lattnerf4111632009-09-11 18:20:26 +0000945
946 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
947 = GetSortedStubs(FnStubs);
948 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
949 Stubs[i].first->print(O, MAI);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000950 O << ":\n" << "\t.indirect_symbol ";
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000951 // Get the MCSymbol without the $stub suffix.
Chris Lattnerf4111632009-09-11 18:20:26 +0000952 Stubs[i].second->print(O, MAI);
Chris Lattner2a3c20b2009-09-11 06:36:33 +0000953 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
954 }
Chris Lattner8f61f982009-06-24 18:24:09 +0000955 O << '\n';
956 }
957
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000958 // Output stubs for external and common global variables.
Chris Lattner8f61f982009-06-24 18:24:09 +0000959 if (!GVStubs.empty()) {
Chris Lattner11e96572009-08-03 21:53:27 +0000960 const MCSection *TheSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000961 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
962 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner11e96572009-08-03 21:53:27 +0000963 SectionKind::getMetadata());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000964 OutStreamer.SwitchSection(TheSection);
Chris Lattnerf4111632009-09-11 18:20:26 +0000965
966 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
967 = GetSortedStubs(FnStubs);
968 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
969 Stubs[i].first->print(O, MAI);
Chris Lattner46091d72009-09-11 06:59:18 +0000970 O << ":\n\t.indirect_symbol ";
Chris Lattnerf4111632009-09-11 18:20:26 +0000971 Stubs[i].second->print(O, MAI);
Chris Lattner46091d72009-09-11 06:59:18 +0000972 O << "\n\t.long\t0\n";
973 }
Chris Lattner8f61f982009-06-24 18:24:09 +0000974 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000975
Evan Chengae94e592008-12-05 01:06:39 +0000976 if (!HiddenGVStubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000977 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner52cff832009-06-24 18:24:42 +0000978 EmitAlignment(2);
Chris Lattnerf4111632009-09-11 18:20:26 +0000979
980 std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
981 = GetSortedStubs(FnStubs);
982 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
983 Stubs[i].first->print(O, MAI);
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000984 O << ":\n" << MAI->getData32bitsDirective();
Chris Lattnerf4111632009-09-11 18:20:26 +0000985 Stubs[i].second->print(O, MAI);
Chris Lattner9e6ffba2009-09-11 07:03:20 +0000986 O << '\n';
987 }
Evan Chengae94e592008-12-05 01:06:39 +0000988 }
989
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000990 // Funny Darwin hack: This flag tells the linker that no global symbols
991 // contain code that falls through to other global symbols (e.g. the obvious
992 // implementation of multiple entry points). If this doesn't occur, the
993 // linker can safely perform dead code stripping. Since LLVM never
994 // generates code that does this, it is always safe to set.
995 O << "\t.subsections_via_symbols\n";
996 } else if (Subtarget->isTargetCygMing()) {
997 // Emit type information for external functions
Chris Lattner1ebd3bf2009-07-09 05:09:24 +0000998 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000999 i != e; ++i) {
1000 O << "\t.def\t " << i->getKeyData()
1001 << ";\t.scl\t" << COFF::C_EXT
1002 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1003 << ";\t.endef\n";
1004 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001005 }
Chris Lattner974469d2009-06-24 05:47:59 +00001006
Chris Lattner0a7befa2009-06-24 18:52:01 +00001007
1008 // Output linker support code for dllexported globals on windows.
Chris Lattner11e96572009-08-03 21:53:27 +00001009 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
1010 // dllexport symbols only exist on coff targets.
1011 TargetLoweringObjectFileCOFF &TLOFMacho =
1012 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
1013
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001014 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
1015 SectionKind::getMetadata()));
Chris Lattner0a7befa2009-06-24 18:52:01 +00001016
1017 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1018 e = DLLExportedGVs.end(); i != e; ++i)
1019 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattner0a7befa2009-06-24 18:52:01 +00001020
1021 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1022 e = DLLExportedFns.end();
1023 i != e; ++i)
1024 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1025 }
1026
Chris Lattner0a7befa2009-06-24 18:52:01 +00001027 // Do common shutdown.
Chris Lattner2b2954f2009-07-27 21:28:04 +00001028 return AsmPrinter::doFinalization(M);
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001029}
1030
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001031// Include the auto-generated portion of the assembly writer.
1032#include "X86GenAsmWriter.inc"