blob: 078750c13041279c79f8a799ec80623940ca04ea [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"
Chris Lattner621c44d2009-08-22 20:48:53 +000022#include "X86MCAsmInfo.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"
Daniel Dunbar34a26db2009-08-31 19:14:05 +000027#include "llvm/ADT/SmallString.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000028#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
Dan Gohman2aa282f2009-08-13 01:36:44 +000030#include "llvm/Assembly/Writer.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000031#include "llvm/MC/MCContext.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000032#include "llvm/MC/MCExpr.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000033#include "llvm/MC/MCInst.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000034#include "llvm/MC/MCSectionMachO.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000035#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000036#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000037#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000038#include "llvm/Support/CommandLine.h"
Edwin Török675d5622009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000040#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Support/Mangler.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000042#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000043#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045using namespace llvm;
46
47STATISTIC(EmittedInsts, "Number of machine instrs printed");
48
Chris Lattner19b1bd52009-06-19 00:47:33 +000049static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
50 cl::Hidden);
51
Chris Lattner2e845952009-06-24 19:44:36 +000052//===----------------------------------------------------------------------===//
53// Primitive Helper Functions.
54//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000055
56void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattnerf39997f2009-08-16 04:28:14 +000057 // FIXME: the actual label generated doesn't matter here! Just mangle in
58 // something unique (the function number) with Private prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000060 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattnerf39997f2009-08-16 04:28:14 +000061 else {
62 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner021ad452009-07-08 23:09:14 +000063 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattnerf39997f2009-08-16 04:28:14 +000064 }
65}
66
67MCSymbol *X86ATTAsmPrinter::GetPICBaseSymbol() {
68 // FIXME: the actual label generated doesn't matter here! Just mangle in
69 // something unique (the function number) with Private prefix.
70 std::string Name;
71
72 if (Subtarget->isTargetDarwin()) {
73 Name = "L" + utostr(getFunctionNumber())+"$pb";
74 } else {
75 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
76 Name = ".Lllvm$" + utostr(getFunctionNumber())+".$piclabel";
Chris Lattner7e5bd8c2009-08-18 05:33:27 +000077 }
Chris Lattnerf39997f2009-08-16 04:28:14 +000078 return OutContext.GetOrCreateSymbol(Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079}
80
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000081static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
82 const TargetData *TD) {
83 X86MachineFunctionInfo Info;
84 uint64_t Size = 0;
85
86 switch (F->getCallingConv()) {
87 case CallingConv::X86_StdCall:
88 Info.setDecorationStyle(StdCall);
89 break;
90 case CallingConv::X86_FastCall:
91 Info.setDecorationStyle(FastCall);
92 break;
93 default:
94 return Info;
95 }
96
97 unsigned argNum = 1;
98 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
99 AI != AE; ++AI, ++argNum) {
100 const Type* Ty = AI->getType();
101
102 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +0000103 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000104 Ty = cast<PointerType>(Ty)->getElementType();
105
106 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000107 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000108 }
109
110 // We're not supporting tooooo huge arguments :)
111 Info.setBytesToPopOnReturn((unsigned int)Size);
112 return Info;
113}
114
Chris Lattner8cfe9142009-07-15 04:55:56 +0000115/// DecorateCygMingName - Query FunctionInfoMap and use this information for
116/// various name decorations for Cygwin and MingW.
117void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
118 const GlobalValue *GV) {
119 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
120
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000121 const Function *F = dyn_cast<Function>(GV);
122 if (!F) return;
123
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000124 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000125 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000126 CygMingStubs.insert(Name);
127
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000128 // We don't want to decorate non-stdcall or non-fastcall functions right now
129 unsigned CC = F->getCallingConv();
130 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
131 return;
132
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000133
134 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000135
136 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000137 if (info_item == FunctionInfoMap.end()) {
138 // Calculate apropriate function info and populate map
139 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
140 Info = &FunctionInfoMap[F];
141 } else {
142 Info = &info_item->second;
143 }
144
145 const FunctionType *FT = F->getFunctionType();
146 switch (Info->getDecorationStyle()) {
147 case None:
148 break;
149 case StdCall:
150 // "Pure" variadic functions do not receive @0 suffix.
151 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
152 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
153 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
154 break;
155 case FastCall:
156 // "Pure" variadic functions do not receive @0 suffix.
157 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
158 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
159 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
160
161 if (Name[0] == '_') {
162 Name[0] = '@';
163 } else {
164 Name = '@' + Name;
165 }
166 break;
167 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000168 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000169 }
170}
171
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000172void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000173 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175
Chris Lattner8cfe9142009-07-15 04:55:56 +0000176 if (Subtarget->isTargetCygMing())
177 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178
Chris Lattner73266f92009-08-19 05:49:37 +0000179 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner1b0ec672009-08-03 22:16:57 +0000180 EmitAlignment(FnAlign, F);
181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000183 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000185 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 break;
187 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 case Function::ExternalLinkage:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000189 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 break;
Dale Johannesenf03574c2009-08-13 00:28:52 +0000191 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000192 case Function::LinkOnceAnyLinkage:
193 case Function::LinkOnceODRLinkage:
194 case Function::WeakAnyLinkage:
195 case Function::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000197 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000198 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000200 O << "\t.globl\t" << CurrentFnName << "\n"
201 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000203 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
205 break;
206 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000207
208 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209
210 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000211 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 else if (Subtarget->isTargetCygMing()) {
213 O << "\t.def\t " << CurrentFnName
214 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000215 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
217 << ";\t.endef\n";
218 }
219
Dan Gohman2aa282f2009-08-13 01:36:44 +0000220 O << CurrentFnName << ':';
221 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000222 O.PadToColumn(MAI->getCommentColumn());
223 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000224 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
225 }
226 O << '\n';
227
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 // Add some workaround for linkonce linkage on Cygwin\MinGW
229 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000230 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000232}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000234/// runOnMachineFunction - This uses the printMachineInstruction()
235/// method to print assembly for each instruction.
236///
237bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
238 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000239 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000240 unsigned CC = F->getCallingConv();
241
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000242 SetupMachineFunction(MF);
243 O << "\n\n";
244
245 // Populate function information map. Actually, We don't want to populate
246 // non-stdcall or non-fastcall functions' information right now.
247 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
248 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
249
250 // Print out constants referenced by the function
251 EmitConstantPool(MF.getConstantPool());
252
253 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000254 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000255
256 // Print the 'header' of function
257 emitFunctionHeader(MF);
258
259 // Emit pre-function debug and/or EH information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000260 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000261 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000264 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
266 I != E; ++I) {
267 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000268 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
269 // This is an entry block or a block that's only reachable via a
270 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
271 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000272 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 O << '\n';
274 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000275 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
276 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000278 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000279 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 printMachineInstruction(II);
281 }
282 }
283
Dale Johannesenf35771f2008-04-08 00:37:56 +0000284 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
285 // If the function is empty, then we need to emit *something*. Otherwise,
286 // the function's label might be associated with something that it wasn't
287 // meant to be associated with. We emit a noop in this situation.
288 // We are assuming inline asms are code.
289 O << "\tnop\n";
290 }
291
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000292 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000293 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000295 // Emit post-function debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000296 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000297 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298
299 // Print out jump tables referenced by the function.
300 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000301
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 // We didn't modify anything.
303 return false;
304}
305
Chris Lattnerae8f9592009-07-13 21:53:19 +0000306/// printSymbolOperand - Print a raw symbol reference operand. This handles
307/// jump tables, constant pools, global address and external symbols, all of
308/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000309void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
310 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000311 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000312 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000313 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000314 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000315 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000316 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000317 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000318 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000319 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000320 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000322 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000323
324 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000325 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
326 Suffix = "$stub";
327 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
328 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
329 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000330 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000331 Suffix = "$non_lazy_ptr";
332
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000333 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000334 if (Subtarget->isTargetCygMing())
335 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000336
Chris Lattner0cc2b792009-07-09 05:42:07 +0000337 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000338 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
339 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000340
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000341 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
342 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
343 GVStubs[Name] = Mang->getMangledName(GV);
344 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
345 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
346 HiddenGVStubs[Name] = Mang->getMangledName(GV);
347 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
348 FnStubs[Name] = Mang->getMangledName(GV);
349
350 // If the name begins with a dollar-sign, enclose it in parens. We do this
351 // to avoid having it look like an integer immediate to the assembler.
352 if (Name[0] == '$')
353 O << '(' << Name << ')';
354 else
355 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000356
Chris Lattner0cc2b792009-07-09 05:42:07 +0000357 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000358 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000360 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000361 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000362 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000363 FnStubs[Name+"$stub"] = Name;
364 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000365 }
366
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000367 // If the name begins with a dollar-sign, enclose it in parens. We do this
368 // to avoid having it look like an integer immediate to the assembler.
369 if (Name[0] == '$')
370 O << '(' << Name << ')';
371 else
372 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000373 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000375 }
Chris Lattner797a0782009-06-27 05:46:24 +0000376
377 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000379 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000380 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000381 break;
382 case X86II::MO_DARWIN_NONLAZY:
383 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
384 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000385 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000386 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000387 break;
388 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
389 O << " + [.-";
390 PrintPICBaseSymbol();
391 O << ']';
392 break;
393 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000394 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
395 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000396 O << '-';
397 PrintPICBaseSymbol();
398 break;
399 case X86II::MO_TLSGD: O << "@TLSGD"; break;
400 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
401 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
402 case X86II::MO_TPOFF: O << "@TPOFF"; break;
403 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
404 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
405 case X86II::MO_GOT: O << "@GOT"; break;
406 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000407 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 }
409}
410
Chris Lattnerae8f9592009-07-13 21:53:19 +0000411/// print_pcrel_imm - This is used to print an immediate value that ends up
412/// being encoded as a pc-relative value. These print slightly differently, for
413/// example, a $ is not emitted.
414void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
415 const MachineOperand &MO = MI->getOperand(OpNo);
416 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000417 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000418 case MachineOperand::MO_Immediate:
419 O << MO.getImm();
420 return;
421 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman2aa282f2009-08-13 01:36:44 +0000422 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000423 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000424 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000425 case MachineOperand::MO_ExternalSymbol:
426 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000427 return;
428 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000429}
430
431
Chris Lattner207a0ca2009-07-13 21:41:08 +0000432
433void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
434 const char *Modifier) {
435 const MachineOperand &MO = MI->getOperand(OpNo);
436 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000437 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000438 case MachineOperand::MO_Register: {
439 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
440 "Virtual registers should not make it this far!");
441 O << '%';
442 unsigned Reg = MO.getReg();
443 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersonac9de032009-08-10 22:56:29 +0000444 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000445 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
446 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner207a0ca2009-07-13 21:41:08 +0000447 Reg = getX86SubSuperRegister(Reg, VT);
448 }
449 O << TRI->getAsmName(Reg);
450 return;
451 }
452
453 case MachineOperand::MO_Immediate:
454 O << '$' << MO.getImm();
455 return;
456
457 case MachineOperand::MO_JumpTableIndex:
458 case MachineOperand::MO_ConstantPoolIndex:
459 case MachineOperand::MO_GlobalAddress:
460 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000461 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000462 printSymbolOperand(MO);
463 break;
464 }
465 }
466}
467
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000469 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 assert(value <= 7 && "Invalid ssecc argument!");
471 switch (value) {
472 case 0: O << "eq"; break;
473 case 1: O << "lt"; break;
474 case 2: O << "le"; break;
475 case 3: O << "unord"; break;
476 case 4: O << "neq"; break;
477 case 5: O << "nlt"; break;
478 case 6: O << "nle"; break;
479 case 7: O << "ord"; break;
480 }
481}
482
Rafael Espindolabca99f72009-04-08 21:14:34 +0000483void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000484 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000485 const MachineOperand &BaseReg = MI->getOperand(Op);
486 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 const MachineOperand &DispSpec = MI->getOperand(Op+3);
488
Chris Lattner791fd482009-07-09 00:27:29 +0000489 // If we really don't want to print out (rip), don't.
490 bool HasBaseReg = BaseReg.getReg() != 0;
491 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
492 BaseReg.getReg() == X86::RIP)
493 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000494
495 // HasParenPart - True if we will print out the () part of the mem ref.
496 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
497
498 if (DispSpec.isImm()) {
499 int DispVal = DispSpec.getImm();
500 if (DispVal || !HasParenPart)
501 O << DispVal;
502 } else {
503 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
504 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000505 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000506 }
507
508 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000509 assert(IndexReg.getReg() != X86::ESP &&
510 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000511
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000512 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000513 if (HasBaseReg)
514 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515
516 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000517 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000518 printOperand(MI, Op+2, Modifier);
519 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000521 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000523 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 }
525}
526
Rafael Espindolabca99f72009-04-08 21:14:34 +0000527void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000528 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000529 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000530 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000531 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000532 printOperand(MI, Op+4, Modifier);
533 O << ':';
534 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000535 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000536}
537
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000538void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000539 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000540 if (!MAI->getSetDirective())
Evan Cheng6fb06762007-11-09 01:32:10 +0000541 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000542
543 // We don't need .set machinery if we have GOT-style relocations
544 if (Subtarget->isPICStyleGOT())
545 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000546
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000547 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng6fb06762007-11-09 01:32:10 +0000548 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000549 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000550 if (Subtarget->isPICStyleRIPRel())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000551 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000552 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000553 else {
554 O << '-';
555 PrintPICBaseSymbol();
556 O << '\n';
557 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000558}
559
Chris Lattner14f791a2009-06-24 19:19:16 +0000560
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000562 PrintPICBaseSymbol();
563 O << '\n';
564 PrintPICBaseSymbol();
565 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566}
567
568
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000569void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
570 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000571 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000572 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000573 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000574
575 O << JTEntryDirective << ' ';
576
Chris Lattner2e9393c2009-07-10 21:00:45 +0000577 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000578 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner4a948932009-07-10 20:47:30 +0000579 << '_' << uid << "_set_" << MBB->getNumber();
580 } else if (Subtarget->isPICStyleGOT()) {
581 printBasicBlockLabel(MBB, false, false, false);
582 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000583 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000584 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000585}
586
Chris Lattner8bb96e42009-06-15 04:42:32 +0000587bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 unsigned Reg = MO.getReg();
589 switch (Mode) {
590 default: return true; // Unknown mode.
591 case 'b': // Print QImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000592 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 break;
594 case 'h': // Print QImode high register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000595 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 break;
597 case 'w': // Print HImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000598 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 break;
600 case 'k': // Print SImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000601 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000603 case 'q': // Print DImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000604 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000605 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 }
607
Evan Cheng00d04a72008-07-07 22:21:06 +0000608 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 return false;
610}
611
612/// PrintAsmOperand - Print out an operand for an inline asm expression.
613///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000614bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000615 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 const char *ExtraCode) {
617 // Does this asm operand have a single letter operand modifier?
618 if (ExtraCode && ExtraCode[0]) {
619 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000620
Chris Lattnerb6047e62009-07-13 21:48:33 +0000621 const MachineOperand &MO = MI->getOperand(OpNo);
622
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 switch (ExtraCode[0]) {
624 default: return true; // Unknown modifier.
Dale Johannesenfabae3e2009-08-19 22:44:41 +0000625 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
626 if (MO.isImm()) {
627 O << MO.getImm();
628 return false;
Dale Johannesen82aca6d2009-08-19 23:44:01 +0000629 }
Dale Johannesenf1f04ec2009-08-25 00:16:14 +0000630 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
631 printSymbolOperand(MO);
632 return false;
633 }
Dale Johannesen82aca6d2009-08-19 23:44:01 +0000634 if (MO.isReg()) {
Dale Johannesenfabae3e2009-08-19 22:44:41 +0000635 O << '(';
636 printOperand(MI, OpNo);
637 O << ')';
638 return false;
639 }
640 return true;
641
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000643 if (MO.isImm())
644 O << MO.getImm();
645 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
646 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000647 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000648 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000650
651 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000652 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000653 O << '*';
654 printOperand(MI, OpNo);
655 return false;
656 }
657 return true;
658
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 case 'b': // Print QImode register
660 case 'h': // Print QImode high register
661 case 'w': // Print HImode register
662 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000663 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000664 if (MO.isReg())
665 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 printOperand(MI, OpNo);
667 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000668
Dale Johannesen375b88b2009-07-07 23:28:22 +0000669 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000670 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000672
Chris Lattnerb6047e62009-07-13 21:48:33 +0000673 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000674 // Note: this is a temporary solution. It should be handled target
675 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000676 if (MO.isImm()) {
677 O << -MO.getImm();
678 return false;
679 }
680 O << '-';
681 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000683
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 printOperand(MI, OpNo);
685 return false;
686}
687
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000688bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000690 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000692 if (ExtraCode && ExtraCode[0]) {
693 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000694
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000695 switch (ExtraCode[0]) {
696 default: return true; // Unknown modifier.
697 case 'b': // Print QImode register
698 case 'h': // Print QImode high register
699 case 'w': // Print HImode register
700 case 'k': // Print SImode register
701 case 'q': // Print SImode register
702 // These only apply to registers, ignore on mem.
703 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000704 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000705 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000706 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000707 }
708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 printMemReference(MI, OpNo);
710 return false;
711}
712
Chris Lattnerf5da5902009-06-20 07:03:18 +0000713static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
714 // Convert registers in the addr mode according to subreg64.
715 for (unsigned i = 0; i != 4; ++i) {
716 if (!MI->getOperand(i).isReg()) continue;
717
718 unsigned Reg = MI->getOperand(i).getReg();
719 if (Reg == 0) continue;
720
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000721 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
Chris Lattnerf5da5902009-06-20 07:03:18 +0000722 }
723}
724
Chris Lattner8fffd452009-08-16 03:12:25 +0000725/// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
726/// MCOperand.
727MCOperand X86ATTAsmPrinter::LowerGlobalAddressOperand(const MachineOperand &MO){
Chris Lattner8fffd452009-08-16 03:12:25 +0000728 const GlobalValue *GV = MO.getGlobal();
729
730 const char *Suffix = "";
731 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
732 Suffix = "$stub";
733 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
734 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
735 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
736 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
737 Suffix = "$non_lazy_ptr";
738
739 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
740 if (Subtarget->isTargetCygMing())
741 DecorateCygMingName(Name, GV);
742
743 // Handle dllimport linkage.
744 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
745 Name = "__imp_" + Name;
746
747 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
748 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
749 GVStubs[Name] = Mang->getMangledName(GV);
750 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
751 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
752 HiddenGVStubs[Name] = Mang->getMangledName(GV);
753 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
754 FnStubs[Name] = Mang->getMangledName(GV);
755
Chris Lattner7e5bd8c2009-08-18 05:33:27 +0000756
757 // Handle target operand flags.
758 // FIXME: This should be common between external symbols, constant pool etc.
759 MCSymbol *NegatedSymbol = 0;
760
761 switch (MO.getTargetFlags()) {
762 default:
763 llvm_unreachable("Unknown target flag on GV operand");
764 case X86II::MO_NO_FLAG: // No flag.
765 break;
766 case X86II::MO_DARWIN_NONLAZY:
767 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
768 case X86II::MO_DLLIMPORT:
769 case X86II::MO_DARWIN_STUB:
770 // These affect the name of the symbol, not any suffix.
771 break;
772 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
773 assert(0 && "Reloc mode unimp!");
774 //O << " + [.-";
775 //PrintPICBaseSymbol();
776 //O << ']';
777 break;
778 case X86II::MO_PIC_BASE_OFFSET:
779 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
780 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
781 // Subtract the pic base.
782 NegatedSymbol = GetPICBaseSymbol();
783 break;
784
785 // FIXME: These probably should be a modifier on the symbol or something??
786 case X86II::MO_TLSGD: Name += "@TLSGD"; break;
787 case X86II::MO_GOTTPOFF: Name += "@GOTTPOFF"; break;
788 case X86II::MO_INDNTPOFF: Name += "@INDNTPOFF"; break;
789 case X86II::MO_TPOFF: Name += "@TPOFF"; break;
790 case X86II::MO_NTPOFF: Name += "@NTPOFF"; break;
791 case X86II::MO_GOTPCREL: Name += "@GOTPCREL"; break;
792 case X86II::MO_GOT: Name += "@GOT"; break;
793 case X86II::MO_GOTOFF: Name += "@GOTOFF"; break;
794 case X86II::MO_PLT: Name += "@PLT"; break;
795 }
796
Chris Lattner8fffd452009-08-16 03:12:25 +0000797 // Create a symbol for the name.
798 MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000799 // FIXME: We would like an efficient form for this, so we don't have to do a
800 // lot of extra uniquing.
801 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, OutContext);
802 if (NegatedSymbol)
803 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(NegatedSymbol,
804 OutContext),
805 OutContext);
Daniel Dunbarbefe9522009-08-31 19:13:47 +0000806 if (MO.getOffset())
807 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(MO.getOffset(),
808 OutContext),
809 OutContext);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000810 return MCOperand::CreateExpr(Expr);
Chris Lattner8fffd452009-08-16 03:12:25 +0000811}
812
Chris Lattnerf39997f2009-08-16 04:28:14 +0000813MCOperand X86ATTAsmPrinter::
Daniel Dunbar34a26db2009-08-31 19:14:05 +0000814LowerExternalSymbolOperand(const MachineOperand &MO) {
Chris Lattnerf39997f2009-08-16 04:28:14 +0000815 std::string Name = Mang->makeNameProper(MO.getSymbolName());
816 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
817 FnStubs[Name+"$stub"] = Name;
818 Name += "$stub";
819 }
820
821 MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000822 // FIXME: We would like an efficient form for this, so we don't have to do a
823 // lot of extra uniquing.
Daniel Dunbarbefe9522009-08-31 19:13:47 +0000824 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, OutContext);
825 if (MO.getOffset())
826 Expr = MCBinaryExpr::CreateAdd(Expr,
827 MCConstantExpr::Create(MO.getOffset(),
828 OutContext),
829 OutContext);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000830 return MCOperand::CreateExpr(Expr);
Chris Lattnerf39997f2009-08-16 04:28:14 +0000831}
832
Daniel Dunbar34a26db2009-08-31 19:14:05 +0000833MCOperand X86ATTAsmPrinter::LowerJumpTableOperand(const MachineOperand &MO) {
834 SmallString<256> Name;
835 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
836 << getFunctionNumber() << '_' << MO.getIndex();
837
838 MCSymbol *NegatedSymbol = 0;
839 switch (MO.getTargetFlags()) {
840 default:
841 llvm_unreachable("Unknown target flag on GV operand");
842 case X86II::MO_PIC_BASE_OFFSET:
843 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
844 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
845 // Subtract the pic base.
846 NegatedSymbol = GetPICBaseSymbol();
847 break;
848 }
849
850 // Create a symbol for the name.
851 MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name.str());
852 // FIXME: We would like an efficient form for this, so we don't have to do a
853 // lot of extra uniquing.
854 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, OutContext);
855 if (NegatedSymbol)
856 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(NegatedSymbol,
857 OutContext),
858 OutContext);
859 return MCOperand::CreateExpr(Expr);
860}
861
Chris Lattnerf39997f2009-08-16 04:28:14 +0000862
Bill Wendlingb3b11262009-02-06 21:45:08 +0000863/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
864/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865///
866void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
867 ++EmittedInsts;
868
Chris Lattnerf39997f2009-08-16 04:28:14 +0000869 if (!NewAsmPrinter) {
870 // Call the autogenerated instruction printer routines.
871 printInstruction(MI);
872 return;
873 }
874
875 MCInst TmpInst;
876
877 switch (MI->getOpcode()) {
878 case TargetInstrInfo::DBG_LABEL:
879 case TargetInstrInfo::EH_LABEL:
880 case TargetInstrInfo::GC_LABEL:
881 printLabel(MI);
882 return;
883 case TargetInstrInfo::INLINEASM:
884 O << '\t';
885 printInlineAsm(MI);
886 return;
Chris Lattnerf39997f2009-08-16 04:28:14 +0000887 case TargetInstrInfo::IMPLICIT_DEF:
888 printImplicitDef(MI);
889 return;
890 case X86::MOVPC32r: {
891 // This is a pseudo op for a two instruction sequence with a label, which
892 // looks like:
893 // call "L1$pb"
894 // "L1$pb":
895 // popl %esi
Chris Lattnerf5da5902009-06-20 07:03:18 +0000896
Chris Lattnerf39997f2009-08-16 04:28:14 +0000897 // Emit the call.
898 MCSymbol *PICBase = GetPICBaseSymbol();
899 TmpInst.setOpcode(X86::CALLpcrel32);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000900 // FIXME: We would like an efficient form for this, so we don't have to do a
901 // lot of extra uniquing.
902 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase,
903 OutContext)));
Chris Lattnerf39997f2009-08-16 04:28:14 +0000904 printInstruction(&TmpInst);
905
906 // Emit the label.
907 OutStreamer.EmitLabel(PICBase);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000908
Chris Lattnerf39997f2009-08-16 04:28:14 +0000909 // popl $reg
910 TmpInst.setOpcode(X86::POP32r);
911 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
912 printInstruction(&TmpInst);
Chris Lattnerf39997f2009-08-16 04:28:14 +0000913 return;
914 }
915 }
916
Chris Lattnerf39997f2009-08-16 04:28:14 +0000917 TmpInst.setOpcode(MI->getOpcode());
918
919 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
920 const MachineOperand &MO = MI->getOperand(i);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000921
Chris Lattnerf39997f2009-08-16 04:28:14 +0000922 MCOperand MCOp;
923 switch (MO.getType()) {
924 default:
925 O.flush();
926 errs() << "Cannot lower operand #" << i << " of :" << *MI;
927 llvm_unreachable("Unimp");
Daniel Dunbar34a26db2009-08-31 19:14:05 +0000928 case MachineOperand::MO_JumpTableIndex:
929 MCOp = LowerJumpTableOperand(MO);
930 break;
Chris Lattnerf39997f2009-08-16 04:28:14 +0000931 case MachineOperand::MO_Register:
932 MCOp = MCOperand::CreateReg(MO.getReg());
933 break;
934 case MachineOperand::MO_Immediate:
935 MCOp = MCOperand::CreateImm(MO.getImm());
936 break;
937 case MachineOperand::MO_MachineBasicBlock:
938 MCOp = MCOperand::CreateMBBLabel(getFunctionNumber(),
939 MO.getMBB()->getNumber());
940 break;
941 case MachineOperand::MO_GlobalAddress:
942 MCOp = LowerGlobalAddressOperand(MO);
943 break;
944 case MachineOperand::MO_ExternalSymbol:
945 MCOp = LowerExternalSymbolOperand(MO);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000946 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000947 }
948
Chris Lattnerf39997f2009-08-16 04:28:14 +0000949 TmpInst.addOperand(MCOp);
Chris Lattner19b1bd52009-06-19 00:47:33 +0000950 }
951
Chris Lattnerf39997f2009-08-16 04:28:14 +0000952 switch (TmpInst.getOpcode()) {
953 case X86::LEA64_32r:
954 // Handle the 'subreg rewriting' for the lea64_32mem operand.
955 lower_lea64_32mem(&TmpInst, 1);
956 break;
957 }
958
959 // FIXME: Convert TmpInst.
960 printInstruction(&TmpInst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961}
962
Chris Lattnerae982212009-07-21 18:38:57 +0000963void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000964 const TargetData *TD = TM.getTargetData();
965
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000966 if (!GVar->hasInitializer())
967 return; // External global require no code
968
969 // Check to see if this is a special global used by LLVM, if so, emit it.
970 if (EmitSpecialLLVMGlobal(GVar)) {
971 if (Subtarget->isTargetDarwin() &&
972 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000973 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000974 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000975 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000976 O << ".reference .destructors_used\n";
977 }
978 return;
979 }
980
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000981 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000982 Constant *C = GVar->getInitializer();
983 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000984 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000985 unsigned Align = TD->getPreferredAlignmentLog(GVar);
986
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000987 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000988
989 if (Subtarget->isTargetELF())
990 O << "\t.type\t" << name << ",@object\n";
991
Chris Lattner7215c7f2009-08-05 05:21:07 +0000992
993 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000994 const MCSection *TheSection =
Chris Lattner7215c7f2009-08-05 05:21:07 +0000995 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000996 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000997
Chris Lattnerdb727932009-08-04 05:35:56 +0000998 // FIXME: get this stuff from section kind flags.
Evan Chengcf84b142009-02-18 02:19:52 +0000999 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +00001000 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +00001001 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001002 if (GVar->hasExternalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001003 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001004 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001005 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001006 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001007 return;
1008 }
1009 }
1010
1011 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +00001012 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001013 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +00001014
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001015 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001016 if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001017 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001018 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001019 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001020 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001021 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001022 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001023 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001024 O << name << ":";
1025 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001026 O.PadToColumn(MAI->getCommentColumn());
1027 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +00001028 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001029 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001030 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001031 EmitGlobalConstant(C);
1032 return;
1033 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001034 O << MAI->getCOMMDirective() << name << ',' << Size;
1035 if (MAI->getCOMMDirectiveTakesAlignment())
1036 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001037 }
1038 } else {
1039 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001040 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001041 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001042 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001043 O << MAI->getCOMMDirective() << name << ',' << Size;
1044 if (MAI->getCOMMDirectiveTakesAlignment())
1045 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001046 }
Evan Cheng11db8142009-03-24 00:17:40 +00001047 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001048 O.PadToColumn(MAI->getCommentColumn());
1049 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +00001050 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001051 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001052 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001053 return;
1054 }
1055 }
1056
1057 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001058 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001059 case GlobalValue::LinkOnceAnyLinkage:
1060 case GlobalValue::LinkOnceODRLinkage:
1061 case GlobalValue::WeakAnyLinkage:
1062 case GlobalValue::WeakODRLinkage:
Dale Johannesenf03574c2009-08-13 00:28:52 +00001063 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001064 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001065 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001066 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001067 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001068 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001069 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001070 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001071 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001072 }
1073 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001074 case GlobalValue::DLLExportLinkage:
1075 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001076 // FIXME: appending linkage variables should go into a section of
1077 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +00001078 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001079 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001080 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001081 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001082 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001083 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001084 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001085 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001086 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001087 }
1088
1089 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001090 O << name << ":";
1091 if (VerboseAsm){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001092 O.PadToColumn(MAI->getCommentColumn());
1093 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +00001094 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001095 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001096 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001097
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001098 EmitGlobalConstant(C);
Dan Gohman2aa282f2009-08-13 01:36:44 +00001099
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001100 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohman2aa282f2009-08-13 01:36:44 +00001101 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001102}
1103
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001104bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001105 // Print out module-level global variables here.
1106 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001107 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001108 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001109 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001110 }
1111
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001112 if (Subtarget->isTargetDarwin()) {
Chris Lattner92873462009-08-03 21:53:27 +00001113 // All darwin targets use mach-o.
1114 TargetLoweringObjectFileMachO &TLOFMacho =
1115 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2e860262009-06-24 18:24:09 +00001116
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001117 // Add the (possibly multiple) personalities to the set of global value
1118 // stubs. Only referenced functions get into the Personalities list.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001119 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001120 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1121 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001122 if (Personalities[i])
1123 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
1124 true /*private label*/)] =
1125 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +00001126 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001127 }
1128
Chris Lattner2e860262009-06-24 18:24:09 +00001129 // Output stubs for dynamically-linked functions
1130 if (!FnStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +00001131 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001132 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
1133 MCSectionMachO::S_SYMBOL_STUBS |
1134 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
1135 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1136 5, SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +00001137 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001138 for (StringMap<std::string>::iterator I = FnStubs.begin(),
1139 E = FnStubs.end(); I != E; ++I)
1140 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
1141 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +00001142 O << '\n';
1143 }
1144
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001145 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001146 if (!GVStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +00001147 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001148 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
1149 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner92873462009-08-03 21:53:27 +00001150 SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +00001151 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001152 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1153 E = GVStubs.end(); I != E; ++I)
1154 O << I->getKeyData() << ":\n\t.indirect_symbol "
1155 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +00001156 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001157
Evan Chenga65854f2008-12-05 01:06:39 +00001158 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001159 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001160 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001161 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1162 E = HiddenGVStubs.end(); I != E; ++I)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001163 O << I->getKeyData() << ":\n" << MAI->getData32bitsDirective()
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001164 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001165 }
1166
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001167 // Funny Darwin hack: This flag tells the linker that no global symbols
1168 // contain code that falls through to other global symbols (e.g. the obvious
1169 // implementation of multiple entry points). If this doesn't occur, the
1170 // linker can safely perform dead code stripping. Since LLVM never
1171 // generates code that does this, it is always safe to set.
1172 O << "\t.subsections_via_symbols\n";
1173 } else if (Subtarget->isTargetCygMing()) {
1174 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +00001175 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001176 i != e; ++i) {
1177 O << "\t.def\t " << i->getKeyData()
1178 << ";\t.scl\t" << COFF::C_EXT
1179 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1180 << ";\t.endef\n";
1181 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001182 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001183
Chris Lattnerdb191f02009-06-24 18:52:01 +00001184
1185 // Output linker support code for dllexported globals on windows.
Chris Lattner92873462009-08-03 21:53:27 +00001186 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
1187 // dllexport symbols only exist on coff targets.
1188 TargetLoweringObjectFileCOFF &TLOFMacho =
1189 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
1190
Chris Lattner73266f92009-08-19 05:49:37 +00001191 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
1192 SectionKind::getMetadata()));
Chris Lattnerdb191f02009-06-24 18:52:01 +00001193
1194 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1195 e = DLLExportedGVs.end(); i != e; ++i)
1196 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattnerdb191f02009-06-24 18:52:01 +00001197
1198 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1199 e = DLLExportedFns.end();
1200 i != e; ++i)
1201 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1202 }
1203
Chris Lattnerdb191f02009-06-24 18:52:01 +00001204 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +00001205 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001206}
1207
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208// Include the auto-generated portion of the assembly writer.
1209#include "X86GenAsmWriter.inc"