blob: 9dffbe831f3c73bd55edbf004ddfb697e8e33822 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
Dan Gohman2aa282f2009-08-13 01:36:44 +000029#include "llvm/Assembly/Writer.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000030#include "llvm/MC/MCContext.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000031#include "llvm/MC/MCInst.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000032#include "llvm/MC/MCSectionMachO.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000033#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000034#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000036#include "llvm/Support/CommandLine.h"
Edwin Török675d5622009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000038#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Support/Mangler.h"
40#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043using namespace llvm;
44
45STATISTIC(EmittedInsts, "Number of machine instrs printed");
46
Chris Lattner19b1bd52009-06-19 00:47:33 +000047static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
48 cl::Hidden);
49
Chris Lattner2e845952009-06-24 19:44:36 +000050//===----------------------------------------------------------------------===//
51// Primitive Helper Functions.
52//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000053
54void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattnerf39997f2009-08-16 04:28:14 +000055 // FIXME: the actual label generated doesn't matter here! Just mangle in
56 // something unique (the function number) with Private prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000058 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattnerf39997f2009-08-16 04:28:14 +000059 else {
60 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner021ad452009-07-08 23:09:14 +000061 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattnerf39997f2009-08-16 04:28:14 +000062 }
63}
64
65MCSymbol *X86ATTAsmPrinter::GetPICBaseSymbol() {
66 // FIXME: the actual label generated doesn't matter here! Just mangle in
67 // something unique (the function number) with Private prefix.
68 std::string Name;
69
70 if (Subtarget->isTargetDarwin()) {
71 Name = "L" + utostr(getFunctionNumber())+"$pb";
72 } else {
73 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
74 Name = ".Lllvm$" + utostr(getFunctionNumber())+".$piclabel";
75 }
76 return OutContext.GetOrCreateSymbol(Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077}
78
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000079static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
80 const TargetData *TD) {
81 X86MachineFunctionInfo Info;
82 uint64_t Size = 0;
83
84 switch (F->getCallingConv()) {
85 case CallingConv::X86_StdCall:
86 Info.setDecorationStyle(StdCall);
87 break;
88 case CallingConv::X86_FastCall:
89 Info.setDecorationStyle(FastCall);
90 break;
91 default:
92 return Info;
93 }
94
95 unsigned argNum = 1;
96 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
97 AI != AE; ++AI, ++argNum) {
98 const Type* Ty = AI->getType();
99
100 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +0000101 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000102 Ty = cast<PointerType>(Ty)->getElementType();
103
104 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000105 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000106 }
107
108 // We're not supporting tooooo huge arguments :)
109 Info.setBytesToPopOnReturn((unsigned int)Size);
110 return Info;
111}
112
Chris Lattner8cfe9142009-07-15 04:55:56 +0000113/// DecorateCygMingName - Query FunctionInfoMap and use this information for
114/// various name decorations for Cygwin and MingW.
115void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
116 const GlobalValue *GV) {
117 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
118
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000119 const Function *F = dyn_cast<Function>(GV);
120 if (!F) return;
121
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000122 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000123 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000124 CygMingStubs.insert(Name);
125
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000126 // We don't want to decorate non-stdcall or non-fastcall functions right now
127 unsigned CC = F->getCallingConv();
128 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
129 return;
130
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000131
132 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000133
134 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000135 if (info_item == FunctionInfoMap.end()) {
136 // Calculate apropriate function info and populate map
137 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
138 Info = &FunctionInfoMap[F];
139 } else {
140 Info = &info_item->second;
141 }
142
143 const FunctionType *FT = F->getFunctionType();
144 switch (Info->getDecorationStyle()) {
145 case None:
146 break;
147 case StdCall:
148 // "Pure" variadic functions do not receive @0 suffix.
149 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
150 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
151 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
152 break;
153 case FastCall:
154 // "Pure" variadic functions do not receive @0 suffix.
155 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
156 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
157 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
158
159 if (Name[0] == '_') {
160 Name[0] = '@';
161 } else {
162 Name = '@' + Name;
163 }
164 break;
165 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000166 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000167 }
168}
169
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000170void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000171 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173
Chris Lattner8cfe9142009-07-15 04:55:56 +0000174 if (Subtarget->isTargetCygMing())
175 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
Chris Lattner2931fe42009-07-29 05:09:30 +0000177 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner1b0ec672009-08-03 22:16:57 +0000178 EmitAlignment(FnAlign, F);
179
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000181 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000183 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 break;
185 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 case Function::ExternalLinkage:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000187 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 break;
Dale Johannesenf03574c2009-08-13 00:28:52 +0000189 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000190 case Function::LinkOnceAnyLinkage:
191 case Function::LinkOnceODRLinkage:
192 case Function::WeakAnyLinkage:
193 case Function::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000195 O << "\t.globl\t" << CurrentFnName << '\n';
196 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000198 O << "\t.globl\t" << CurrentFnName << "\n"
199 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000201 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 }
203 break;
204 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000205
206 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
208 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000209 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 else if (Subtarget->isTargetCygMing()) {
211 O << "\t.def\t " << CurrentFnName
212 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000213 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
215 << ";\t.endef\n";
216 }
217
Dan Gohman2aa282f2009-08-13 01:36:44 +0000218 O << CurrentFnName << ':';
219 if (VerboseAsm) {
220 O.PadToColumn(TAI->getCommentColumn(), 1);
221 O << TAI->getCommentString() << ' ';
222 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
223 }
224 O << '\n';
225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 // Add some workaround for linkonce linkage on Cygwin\MinGW
227 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000228 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000230}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000232/// runOnMachineFunction - This uses the printMachineInstruction()
233/// method to print assembly for each instruction.
234///
235bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
236 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000237 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000238 unsigned CC = F->getCallingConv();
239
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000240 SetupMachineFunction(MF);
241 O << "\n\n";
242
243 // Populate function information map. Actually, We don't want to populate
244 // non-stdcall or non-fastcall functions' information right now.
245 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
246 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
247
248 // Print out constants referenced by the function
249 EmitConstantPool(MF.getConstantPool());
250
251 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000252 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000253
254 // Print the 'header' of function
255 emitFunctionHeader(MF);
256
257 // Emit pre-function debug and/or EH information.
258 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000259 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000260
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000262 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
264 I != E; ++I) {
265 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000266 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
267 // This is an entry block or a block that's only reachable via a
268 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
269 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000270 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 O << '\n';
272 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000273 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
274 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000276 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000277 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 printMachineInstruction(II);
279 }
280 }
281
Dale Johannesenf35771f2008-04-08 00:37:56 +0000282 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
283 // If the function is empty, then we need to emit *something*. Otherwise,
284 // the function's label might be associated with something that it wasn't
285 // meant to be associated with. We emit a noop in this situation.
286 // We are assuming inline asms are code.
287 O << "\tnop\n";
288 }
289
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000291 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000293 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000294 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000295 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296
297 // Print out jump tables referenced by the function.
298 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 // We didn't modify anything.
301 return false;
302}
303
Chris Lattnerae8f9592009-07-13 21:53:19 +0000304/// printSymbolOperand - Print a raw symbol reference operand. This handles
305/// jump tables, constant pools, global address and external symbols, all of
306/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000307void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
308 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000309 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000310 case MachineOperand::MO_JumpTableIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000311 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000312 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000313 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000314 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000315 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000316 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000317 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000318 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000320 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000321
322 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000323 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
324 Suffix = "$stub";
325 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
326 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
327 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000328 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000329 Suffix = "$non_lazy_ptr";
330
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000331 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000332 if (Subtarget->isTargetCygMing())
333 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000334
Chris Lattner0cc2b792009-07-09 05:42:07 +0000335 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000336 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
337 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000338
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000339 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
340 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
341 GVStubs[Name] = Mang->getMangledName(GV);
342 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
343 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
344 HiddenGVStubs[Name] = Mang->getMangledName(GV);
345 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
346 FnStubs[Name] = Mang->getMangledName(GV);
347
348 // If the name begins with a dollar-sign, enclose it in parens. We do this
349 // to avoid having it look like an integer immediate to the assembler.
350 if (Name[0] == '$')
351 O << '(' << Name << ')';
352 else
353 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000354
Chris Lattner0cc2b792009-07-09 05:42:07 +0000355 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000356 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000358 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000359 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000360 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000361 FnStubs[Name+"$stub"] = Name;
362 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000363 }
364
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000365 // If the name begins with a dollar-sign, enclose it in parens. We do this
366 // to avoid having it look like an integer immediate to the assembler.
367 if (Name[0] == '$')
368 O << '(' << Name << ')';
369 else
370 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000371 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000373 }
Chris Lattner797a0782009-06-27 05:46:24 +0000374
375 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000377 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000378 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000379 break;
380 case X86II::MO_DARWIN_NONLAZY:
381 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
382 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000383 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000384 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000385 break;
386 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
387 O << " + [.-";
388 PrintPICBaseSymbol();
389 O << ']';
390 break;
391 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000392 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
393 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000394 O << '-';
395 PrintPICBaseSymbol();
396 break;
397 case X86II::MO_TLSGD: O << "@TLSGD"; break;
398 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
399 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
400 case X86II::MO_TPOFF: O << "@TPOFF"; break;
401 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
402 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
403 case X86II::MO_GOT: O << "@GOT"; break;
404 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000405 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 }
407}
408
Chris Lattnerae8f9592009-07-13 21:53:19 +0000409/// print_pcrel_imm - This is used to print an immediate value that ends up
410/// being encoded as a pc-relative value. These print slightly differently, for
411/// example, a $ is not emitted.
412void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
413 const MachineOperand &MO = MI->getOperand(OpNo);
414 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000415 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000416 case MachineOperand::MO_Immediate:
417 O << MO.getImm();
418 return;
419 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman2aa282f2009-08-13 01:36:44 +0000420 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000421 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000422 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000423 case MachineOperand::MO_ExternalSymbol:
424 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000425 return;
426 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000427}
428
429
Chris Lattner207a0ca2009-07-13 21:41:08 +0000430
431void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
432 const char *Modifier) {
433 const MachineOperand &MO = MI->getOperand(OpNo);
434 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000435 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000436 case MachineOperand::MO_Register: {
437 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
438 "Virtual registers should not make it this far!");
439 O << '%';
440 unsigned Reg = MO.getReg();
441 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersonac9de032009-08-10 22:56:29 +0000442 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000443 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
444 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner207a0ca2009-07-13 21:41:08 +0000445 Reg = getX86SubSuperRegister(Reg, VT);
446 }
447 O << TRI->getAsmName(Reg);
448 return;
449 }
450
451 case MachineOperand::MO_Immediate:
452 O << '$' << MO.getImm();
453 return;
454
455 case MachineOperand::MO_JumpTableIndex:
456 case MachineOperand::MO_ConstantPoolIndex:
457 case MachineOperand::MO_GlobalAddress:
458 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000459 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000460 printSymbolOperand(MO);
461 break;
462 }
463 }
464}
465
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000467 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 assert(value <= 7 && "Invalid ssecc argument!");
469 switch (value) {
470 case 0: O << "eq"; break;
471 case 1: O << "lt"; break;
472 case 2: O << "le"; break;
473 case 3: O << "unord"; break;
474 case 4: O << "neq"; break;
475 case 5: O << "nlt"; break;
476 case 6: O << "nle"; break;
477 case 7: O << "ord"; break;
478 }
479}
480
Rafael Espindolabca99f72009-04-08 21:14:34 +0000481void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000482 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000483 const MachineOperand &BaseReg = MI->getOperand(Op);
484 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 const MachineOperand &DispSpec = MI->getOperand(Op+3);
486
Chris Lattner791fd482009-07-09 00:27:29 +0000487 // If we really don't want to print out (rip), don't.
488 bool HasBaseReg = BaseReg.getReg() != 0;
489 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
490 BaseReg.getReg() == X86::RIP)
491 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000492
493 // HasParenPart - True if we will print out the () part of the mem ref.
494 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
495
496 if (DispSpec.isImm()) {
497 int DispVal = DispSpec.getImm();
498 if (DispVal || !HasParenPart)
499 O << DispVal;
500 } else {
501 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
502 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000503 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000504 }
505
506 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000507 assert(IndexReg.getReg() != X86::ESP &&
508 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000509
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000510 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000511 if (HasBaseReg)
512 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513
514 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000515 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000516 printOperand(MI, Op+2, Modifier);
517 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000519 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000521 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 }
523}
524
Rafael Espindolabca99f72009-04-08 21:14:34 +0000525void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000526 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000527 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000528 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000529 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000530 printOperand(MI, Op+4, Modifier);
531 O << ':';
532 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000533 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000534}
535
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000536void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000537 const MachineBasicBlock *MBB) const {
538 if (!TAI->getSetDirective())
539 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000540
541 // We don't need .set machinery if we have GOT-style relocations
542 if (Subtarget->isPICStyleGOT())
543 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000544
Evan Cheng6fb06762007-11-09 01:32:10 +0000545 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
546 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000547 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000548 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000549 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000550 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000551 else {
552 O << '-';
553 PrintPICBaseSymbol();
554 O << '\n';
555 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000556}
557
Chris Lattner14f791a2009-06-24 19:19:16 +0000558
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000560 PrintPICBaseSymbol();
561 O << '\n';
562 PrintPICBaseSymbol();
563 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564}
565
566
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000567void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
568 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000569 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000570 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
571 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
572
573 O << JTEntryDirective << ' ';
574
Chris Lattner2e9393c2009-07-10 21:00:45 +0000575 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000576 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
577 << '_' << uid << "_set_" << MBB->getNumber();
578 } else if (Subtarget->isPICStyleGOT()) {
579 printBasicBlockLabel(MBB, false, false, false);
580 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000581 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000582 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000583}
584
Chris Lattner8bb96e42009-06-15 04:42:32 +0000585bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 unsigned Reg = MO.getReg();
587 switch (Mode) {
588 default: return true; // Unknown mode.
589 case 'b': // Print QImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000590 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 break;
592 case 'h': // Print QImode high register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000593 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 break;
595 case 'w': // Print HImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000596 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 break;
598 case 'k': // Print SImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000599 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000601 case 'q': // Print DImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000602 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000603 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 }
605
Evan Cheng00d04a72008-07-07 22:21:06 +0000606 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 return false;
608}
609
610/// PrintAsmOperand - Print out an operand for an inline asm expression.
611///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000612bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000613 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 const char *ExtraCode) {
615 // Does this asm operand have a single letter operand modifier?
616 if (ExtraCode && ExtraCode[0]) {
617 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000618
Chris Lattnerb6047e62009-07-13 21:48:33 +0000619 const MachineOperand &MO = MI->getOperand(OpNo);
620
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 switch (ExtraCode[0]) {
622 default: return true; // Unknown modifier.
623 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000624 if (MO.isImm())
625 O << MO.getImm();
626 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
627 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000628 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000629 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000631
632 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000633 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000634 O << '*';
635 printOperand(MI, OpNo);
636 return false;
637 }
638 return true;
639
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 case 'b': // Print QImode register
641 case 'h': // Print QImode high register
642 case 'w': // Print HImode register
643 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000644 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000645 if (MO.isReg())
646 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 printOperand(MI, OpNo);
648 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000649
Dale Johannesen375b88b2009-07-07 23:28:22 +0000650 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000651 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000653
Chris Lattnerb6047e62009-07-13 21:48:33 +0000654 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000655 // Note: this is a temporary solution. It should be handled target
656 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000657 if (MO.isImm()) {
658 O << -MO.getImm();
659 return false;
660 }
661 O << '-';
662 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000664
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 printOperand(MI, OpNo);
666 return false;
667}
668
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000669bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000671 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000673 if (ExtraCode && ExtraCode[0]) {
674 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000675
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000676 switch (ExtraCode[0]) {
677 default: return true; // Unknown modifier.
678 case 'b': // Print QImode register
679 case 'h': // Print QImode high register
680 case 'w': // Print HImode register
681 case 'k': // Print SImode register
682 case 'q': // Print SImode register
683 // These only apply to registers, ignore on mem.
684 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000685 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000686 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000687 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000688 }
689 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 printMemReference(MI, OpNo);
691 return false;
692}
693
Chris Lattnerf5da5902009-06-20 07:03:18 +0000694static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
695 // Convert registers in the addr mode according to subreg64.
696 for (unsigned i = 0; i != 4; ++i) {
697 if (!MI->getOperand(i).isReg()) continue;
698
699 unsigned Reg = MI->getOperand(i).getReg();
700 if (Reg == 0) continue;
701
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000702 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
Chris Lattnerf5da5902009-06-20 07:03:18 +0000703 }
704}
705
Chris Lattner8fffd452009-08-16 03:12:25 +0000706/// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
707/// MCOperand.
708MCOperand X86ATTAsmPrinter::LowerGlobalAddressOperand(const MachineOperand &MO){
Chris Lattner8fffd452009-08-16 03:12:25 +0000709 const GlobalValue *GV = MO.getGlobal();
710
711 const char *Suffix = "";
712 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
713 Suffix = "$stub";
714 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
715 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
716 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
717 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
718 Suffix = "$non_lazy_ptr";
719
720 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
721 if (Subtarget->isTargetCygMing())
722 DecorateCygMingName(Name, GV);
723
724 // Handle dllimport linkage.
725 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
726 Name = "__imp_" + Name;
727
728 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
729 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
730 GVStubs[Name] = Mang->getMangledName(GV);
731 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
732 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
733 HiddenGVStubs[Name] = Mang->getMangledName(GV);
734 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
735 FnStubs[Name] = Mang->getMangledName(GV);
736
737 // Create a symbol for the name.
738 MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
739 return MCOperand::CreateMCValue(MCValue::get(Sym, 0, MO.getOffset()));
740}
741
Chris Lattnerf39997f2009-08-16 04:28:14 +0000742MCOperand X86ATTAsmPrinter::
743LowerExternalSymbolOperand(const MachineOperand &MO){
744 std::string Name = Mang->makeNameProper(MO.getSymbolName());
745 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
746 FnStubs[Name+"$stub"] = Name;
747 Name += "$stub";
748 }
749
750 MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
751 return MCOperand::CreateMCValue(MCValue::get(Sym, 0, MO.getOffset()));
752}
753
754
Bill Wendlingb3b11262009-02-06 21:45:08 +0000755/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
756/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757///
758void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
759 ++EmittedInsts;
760
Chris Lattnerf39997f2009-08-16 04:28:14 +0000761 if (!NewAsmPrinter) {
762 // Call the autogenerated instruction printer routines.
763 printInstruction(MI);
764 return;
765 }
766
767 MCInst TmpInst;
768
769 switch (MI->getOpcode()) {
770 case TargetInstrInfo::DBG_LABEL:
771 case TargetInstrInfo::EH_LABEL:
772 case TargetInstrInfo::GC_LABEL:
773 printLabel(MI);
774 return;
775 case TargetInstrInfo::INLINEASM:
776 O << '\t';
777 printInlineAsm(MI);
778 return;
779 case TargetInstrInfo::DECLARE:
780 printDeclare(MI);
781 return;
782 case TargetInstrInfo::IMPLICIT_DEF:
783 printImplicitDef(MI);
784 return;
785 case X86::MOVPC32r: {
786 // This is a pseudo op for a two instruction sequence with a label, which
787 // looks like:
788 // call "L1$pb"
789 // "L1$pb":
790 // popl %esi
Chris Lattnerf5da5902009-06-20 07:03:18 +0000791
Chris Lattnerf39997f2009-08-16 04:28:14 +0000792 // Emit the call.
793 MCSymbol *PICBase = GetPICBaseSymbol();
794 TmpInst.setOpcode(X86::CALLpcrel32);
795 TmpInst.addOperand(MCOperand::CreateMCValue(MCValue::get(PICBase)));
796 printInstruction(&TmpInst);
797
798 // Emit the label.
799 OutStreamer.EmitLabel(PICBase);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000800
Chris Lattnerf39997f2009-08-16 04:28:14 +0000801 // popl $reg
802 TmpInst.setOpcode(X86::POP32r);
803 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
804 printInstruction(&TmpInst);
805 O << "OLD: ";
806 // Call the autogenerated instruction printer routines.
807 printInstruction(MI);
808 return;
809 }
810 }
811
812 O << "NEW: ";
813
814 TmpInst.setOpcode(MI->getOpcode());
815
816 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
817 const MachineOperand &MO = MI->getOperand(i);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000818
Chris Lattnerf39997f2009-08-16 04:28:14 +0000819 MCOperand MCOp;
820 switch (MO.getType()) {
821 default:
822 O.flush();
823 errs() << "Cannot lower operand #" << i << " of :" << *MI;
824 llvm_unreachable("Unimp");
825 case MachineOperand::MO_Register:
826 MCOp = MCOperand::CreateReg(MO.getReg());
827 break;
828 case MachineOperand::MO_Immediate:
829 MCOp = MCOperand::CreateImm(MO.getImm());
830 break;
831 case MachineOperand::MO_MachineBasicBlock:
832 MCOp = MCOperand::CreateMBBLabel(getFunctionNumber(),
833 MO.getMBB()->getNumber());
834 break;
835 case MachineOperand::MO_GlobalAddress:
836 MCOp = LowerGlobalAddressOperand(MO);
837 break;
838 case MachineOperand::MO_ExternalSymbol:
839 MCOp = LowerExternalSymbolOperand(MO);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000840 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000841 }
842
Chris Lattnerf39997f2009-08-16 04:28:14 +0000843 TmpInst.addOperand(MCOp);
Chris Lattner19b1bd52009-06-19 00:47:33 +0000844 }
845
Chris Lattnerf39997f2009-08-16 04:28:14 +0000846 switch (TmpInst.getOpcode()) {
847 case X86::LEA64_32r:
848 // Handle the 'subreg rewriting' for the lea64_32mem operand.
849 lower_lea64_32mem(&TmpInst, 1);
850 break;
851 }
852
853 // FIXME: Convert TmpInst.
854 printInstruction(&TmpInst);
855 O << "OLD: ";
856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 // Call the autogenerated instruction printer routines.
858 printInstruction(MI);
859}
860
Chris Lattnerae982212009-07-21 18:38:57 +0000861void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000862 const TargetData *TD = TM.getTargetData();
863
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000864 if (!GVar->hasInitializer())
865 return; // External global require no code
866
867 // Check to see if this is a special global used by LLVM, if so, emit it.
868 if (EmitSpecialLLVMGlobal(GVar)) {
869 if (Subtarget->isTargetDarwin() &&
870 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000871 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000873 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000874 O << ".reference .destructors_used\n";
875 }
876 return;
877 }
878
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000879 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000880 Constant *C = GVar->getInitializer();
881 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000882 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000883 unsigned Align = TD->getPreferredAlignmentLog(GVar);
884
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000885 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000886
887 if (Subtarget->isTargetELF())
888 O << "\t.type\t" << name << ",@object\n";
889
Chris Lattner7215c7f2009-08-05 05:21:07 +0000890
891 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000892 const MCSection *TheSection =
Chris Lattner7215c7f2009-08-05 05:21:07 +0000893 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000894 SwitchToSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000895
Chris Lattnerdb727932009-08-04 05:35:56 +0000896 // FIXME: get this stuff from section kind flags.
Evan Chengcf84b142009-02-18 02:19:52 +0000897 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000898 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000899 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000900 if (GVar->hasExternalLinkage()) {
901 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000902 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000903 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000904 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000905 return;
906 }
907 }
908
909 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000910 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000911 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000912
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000913 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000914 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000915 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000916 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000917 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000918 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000919 O << "\t.globl " << name << '\n'
920 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000921 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000922 O << name << ":";
923 if (VerboseAsm) {
Dan Gohman167ff152009-08-12 18:55:32 +0000924 O.PadToColumn(TAI->getCommentColumn(), 1);
925 O << TAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000926 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000927 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000928 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000929 EmitGlobalConstant(C);
930 return;
931 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000932 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000933 if (TAI->getCOMMDirectiveTakesAlignment())
934 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000935 }
936 } else {
937 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000938 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000939 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000940 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000941 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000942 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000943 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000944 }
Evan Cheng11db8142009-03-24 00:17:40 +0000945 if (VerboseAsm) {
Dan Gohman167ff152009-08-12 18:55:32 +0000946 O.PadToColumn(TAI->getCommentColumn(), 1);
947 O << TAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000948 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000949 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000950 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000951 return;
952 }
953 }
954
955 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000956 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000957 case GlobalValue::LinkOnceAnyLinkage:
958 case GlobalValue::LinkOnceODRLinkage:
959 case GlobalValue::WeakAnyLinkage:
960 case GlobalValue::WeakODRLinkage:
Dale Johannesenf03574c2009-08-13 00:28:52 +0000961 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000962 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000963 O << "\t.globl " << name << '\n'
964 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000965 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000966 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000967 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000968 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000969 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000970 }
971 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000972 case GlobalValue::DLLExportLinkage:
973 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000974 // FIXME: appending linkage variables should go into a section of
975 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000976 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000977 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000978 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000979 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000980 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000981 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000982 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000983 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000984 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000985 }
986
987 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000988 O << name << ":";
989 if (VerboseAsm){
Dan Gohman167ff152009-08-12 18:55:32 +0000990 O.PadToColumn(TAI->getCommentColumn(), 1);
991 O << TAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000992 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000993 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000994 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000995
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000996 EmitGlobalConstant(C);
Dan Gohman2aa282f2009-08-13 01:36:44 +0000997
998 if (TAI->hasDotTypeDotSizeDirective())
999 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001000}
1001
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001002bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001003 // Print out module-level global variables here.
1004 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001005 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001006 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001007 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001008 }
1009
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001010 if (Subtarget->isTargetDarwin()) {
Chris Lattner92873462009-08-03 21:53:27 +00001011 // All darwin targets use mach-o.
1012 TargetLoweringObjectFileMachO &TLOFMacho =
1013 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2e860262009-06-24 18:24:09 +00001014
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001015 // Add the (possibly multiple) personalities to the set of global value
1016 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001017 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001018 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1019 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001020 if (Personalities[i])
1021 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
1022 true /*private label*/)] =
1023 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +00001024 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001025 }
1026
Chris Lattner2e860262009-06-24 18:24:09 +00001027 // Output stubs for dynamically-linked functions
1028 if (!FnStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +00001029 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001030 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
1031 MCSectionMachO::S_SYMBOL_STUBS |
1032 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
1033 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1034 5, SectionKind::getMetadata());
Chris Lattner92873462009-08-03 21:53:27 +00001035 SwitchToSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001036 for (StringMap<std::string>::iterator I = FnStubs.begin(),
1037 E = FnStubs.end(); I != E; ++I)
1038 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
1039 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +00001040 O << '\n';
1041 }
1042
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001043 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001044 if (!GVStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +00001045 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001046 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
1047 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner92873462009-08-03 21:53:27 +00001048 SectionKind::getMetadata());
1049 SwitchToSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001050 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1051 E = GVStubs.end(); I != E; ++I)
1052 O << I->getKeyData() << ":\n\t.indirect_symbol "
1053 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +00001054 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001055
Evan Chenga65854f2008-12-05 01:06:39 +00001056 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001057 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001058 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001059 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1060 E = HiddenGVStubs.end(); I != E; ++I)
1061 O << I->getKeyData() << ":\n" << TAI->getData32bitsDirective()
1062 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001063 }
1064
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001065 // Funny Darwin hack: This flag tells the linker that no global symbols
1066 // contain code that falls through to other global symbols (e.g. the obvious
1067 // implementation of multiple entry points). If this doesn't occur, the
1068 // linker can safely perform dead code stripping. Since LLVM never
1069 // generates code that does this, it is always safe to set.
1070 O << "\t.subsections_via_symbols\n";
1071 } else if (Subtarget->isTargetCygMing()) {
1072 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +00001073 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001074 i != e; ++i) {
1075 O << "\t.def\t " << i->getKeyData()
1076 << ";\t.scl\t" << COFF::C_EXT
1077 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1078 << ";\t.endef\n";
1079 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001080 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001081
Chris Lattnerdb191f02009-06-24 18:52:01 +00001082
1083 // Output linker support code for dllexported globals on windows.
Chris Lattner92873462009-08-03 21:53:27 +00001084 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
1085 // dllexport symbols only exist on coff targets.
1086 TargetLoweringObjectFileCOFF &TLOFMacho =
1087 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
1088
1089 SwitchToSection(TLOFMacho.getCOFFSection(".section .drectve", true,
1090 SectionKind::getMetadata()));
Chris Lattnerdb191f02009-06-24 18:52:01 +00001091
1092 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1093 e = DLLExportedGVs.end(); i != e; ++i)
1094 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattnerdb191f02009-06-24 18:52:01 +00001095
1096 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1097 e = DLLExportedFns.end();
1098 i != e; ++i)
1099 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1100 }
1101
Chris Lattnerdb191f02009-06-24 18:52:01 +00001102 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +00001103 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001104}
1105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106// Include the auto-generated portion of the assembly writer.
1107#include "X86GenAsmWriter.inc"