blob: 2058d7d0a053ccb3ddceb46be28b87196c03da2a [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"
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"
Daniel Dunbar6e966212009-08-31 08:08:38 +000031#include "llvm/MC/MCExpr.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000032#include "llvm/MC/MCInst.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000033#include "llvm/MC/MCSectionMachO.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000034#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000035#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000036#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000037#include "llvm/Support/CommandLine.h"
Edwin Török675d5622009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000039#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/Support/Mangler.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000041#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000042#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044using namespace llvm;
45
46STATISTIC(EmittedInsts, "Number of machine instrs printed");
47
Chris Lattner19b1bd52009-06-19 00:47:33 +000048static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
49 cl::Hidden);
50
Chris Lattner2e845952009-06-24 19:44:36 +000051//===----------------------------------------------------------------------===//
52// Primitive Helper Functions.
53//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000054
55void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Chris Lattnerf39997f2009-08-16 04:28:14 +000056 // FIXME: the actual label generated doesn't matter here! Just mangle in
57 // something unique (the function number) with Private prefix.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000059 O << "\"L" << getFunctionNumber() << "$pb\"";
Chris Lattnerf39997f2009-08-16 04:28:14 +000060 else {
61 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
Chris Lattner021ad452009-07-08 23:09:14 +000062 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Chris Lattnerf39997f2009-08-16 04:28:14 +000063 }
64}
65
66MCSymbol *X86ATTAsmPrinter::GetPICBaseSymbol() {
67 // FIXME: the actual label generated doesn't matter here! Just mangle in
68 // something unique (the function number) with Private prefix.
69 std::string Name;
70
71 if (Subtarget->isTargetDarwin()) {
72 Name = "L" + utostr(getFunctionNumber())+"$pb";
73 } else {
74 assert(Subtarget->isTargetELF() && "Don't know how to print PIC label!");
75 Name = ".Lllvm$" + utostr(getFunctionNumber())+".$piclabel";
Chris Lattner7e5bd8c2009-08-18 05:33:27 +000076 }
Chris Lattnerf39997f2009-08-16 04:28:14 +000077 return OutContext.GetOrCreateSymbol(Name);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078}
79
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000080static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
81 const TargetData *TD) {
82 X86MachineFunctionInfo Info;
83 uint64_t Size = 0;
84
85 switch (F->getCallingConv()) {
86 case CallingConv::X86_StdCall:
87 Info.setDecorationStyle(StdCall);
88 break;
89 case CallingConv::X86_FastCall:
90 Info.setDecorationStyle(FastCall);
91 break;
92 default:
93 return Info;
94 }
95
96 unsigned argNum = 1;
97 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
98 AI != AE; ++AI, ++argNum) {
99 const Type* Ty = AI->getType();
100
101 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +0000102 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000103 Ty = cast<PointerType>(Ty)->getElementType();
104
105 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000106 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000107 }
108
109 // We're not supporting tooooo huge arguments :)
110 Info.setBytesToPopOnReturn((unsigned int)Size);
111 return Info;
112}
113
Chris Lattner8cfe9142009-07-15 04:55:56 +0000114/// DecorateCygMingName - Query FunctionInfoMap and use this information for
115/// various name decorations for Cygwin and MingW.
116void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
117 const GlobalValue *GV) {
118 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
119
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000120 const Function *F = dyn_cast<Function>(GV);
121 if (!F) return;
122
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000123 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000124 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000125 CygMingStubs.insert(Name);
126
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000127 // We don't want to decorate non-stdcall or non-fastcall functions right now
128 unsigned CC = F->getCallingConv();
129 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
130 return;
131
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000132
133 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000134
135 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000136 if (info_item == FunctionInfoMap.end()) {
137 // Calculate apropriate function info and populate map
138 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
139 Info = &FunctionInfoMap[F];
140 } else {
141 Info = &info_item->second;
142 }
143
144 const FunctionType *FT = F->getFunctionType();
145 switch (Info->getDecorationStyle()) {
146 case None:
147 break;
148 case StdCall:
149 // "Pure" variadic functions do not receive @0 suffix.
150 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
151 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
152 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
153 break;
154 case FastCall:
155 // "Pure" variadic functions do not receive @0 suffix.
156 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
157 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
158 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
159
160 if (Name[0] == '_') {
161 Name[0] = '@';
162 } else {
163 Name = '@' + Name;
164 }
165 break;
166 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000167 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000168 }
169}
170
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000171void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000172 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174
Chris Lattner8cfe9142009-07-15 04:55:56 +0000175 if (Subtarget->isTargetCygMing())
176 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Chris Lattner73266f92009-08-19 05:49:37 +0000178 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner1b0ec672009-08-03 22:16:57 +0000179 EmitAlignment(FnAlign, F);
180
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000182 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000184 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 break;
186 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 case Function::ExternalLinkage:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 break;
Dale Johannesenf03574c2009-08-13 00:28:52 +0000190 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000191 case Function::LinkOnceAnyLinkage:
192 case Function::LinkOnceODRLinkage:
193 case Function::WeakAnyLinkage:
194 case Function::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000196 O << "\t.globl\t" << CurrentFnName << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000197 O << MAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000199 O << "\t.globl\t" << CurrentFnName << "\n"
200 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000202 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 }
204 break;
205 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000206
207 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208
209 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000210 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 else if (Subtarget->isTargetCygMing()) {
212 O << "\t.def\t " << CurrentFnName
213 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000214 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
216 << ";\t.endef\n";
217 }
218
Dan Gohman2aa282f2009-08-13 01:36:44 +0000219 O << CurrentFnName << ':';
220 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000221 O.PadToColumn(MAI->getCommentColumn());
222 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000223 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
224 }
225 O << '\n';
226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 // Add some workaround for linkonce linkage on Cygwin\MinGW
228 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000229 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000231}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000233/// runOnMachineFunction - This uses the printMachineInstruction()
234/// method to print assembly for each instruction.
235///
236bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
237 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000238 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000239 unsigned CC = F->getCallingConv();
240
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000241 SetupMachineFunction(MF);
242 O << "\n\n";
243
244 // Populate function information map. Actually, We don't want to populate
245 // non-stdcall or non-fastcall functions' information right now.
246 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
247 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
248
249 // Print out constants referenced by the function
250 EmitConstantPool(MF.getConstantPool());
251
252 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000253 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000254
255 // Print the 'header' of function
256 emitFunctionHeader(MF);
257
258 // Emit pre-function debug and/or EH information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000259 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000260 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000261
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000263 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
265 I != E; ++I) {
266 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000267 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
268 // This is an entry block or a block that's only reachable via a
269 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
270 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000271 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 O << '\n';
273 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000274 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
275 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000277 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000278 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 printMachineInstruction(II);
280 }
281 }
282
Dale Johannesenf35771f2008-04-08 00:37:56 +0000283 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
284 // If the function is empty, then we need to emit *something*. Otherwise,
285 // the function's label might be associated with something that it wasn't
286 // meant to be associated with. We emit a noop in this situation.
287 // We are assuming inline asms are code.
288 O << "\tnop\n";
289 }
290
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000291 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000292 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000294 // Emit post-function debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000295 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000296 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
298 // Print out jump tables referenced by the function.
299 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 // We didn't modify anything.
302 return false;
303}
304
Chris Lattnerae8f9592009-07-13 21:53:19 +0000305/// printSymbolOperand - Print a raw symbol reference operand. This handles
306/// jump tables, constant pools, global address and external symbols, all of
307/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000308void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
309 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000310 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000311 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000312 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000313 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000314 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000315 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000316 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000317 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000318 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000319 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000321 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000322
323 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000324 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
325 Suffix = "$stub";
326 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
327 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
328 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000329 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000330 Suffix = "$non_lazy_ptr";
331
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000332 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000333 if (Subtarget->isTargetCygMing())
334 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000335
Chris Lattner0cc2b792009-07-09 05:42:07 +0000336 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000337 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
338 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000339
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000340 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
341 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
342 GVStubs[Name] = Mang->getMangledName(GV);
343 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
344 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
345 HiddenGVStubs[Name] = Mang->getMangledName(GV);
346 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
347 FnStubs[Name] = Mang->getMangledName(GV);
348
349 // If the name begins with a dollar-sign, enclose it in parens. We do this
350 // to avoid having it look like an integer immediate to the assembler.
351 if (Name[0] == '$')
352 O << '(' << Name << ')';
353 else
354 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000355
Chris Lattner0cc2b792009-07-09 05:42:07 +0000356 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000357 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000359 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000360 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000361 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000362 FnStubs[Name+"$stub"] = Name;
363 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000364 }
365
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000366 // If the name begins with a dollar-sign, enclose it in parens. We do this
367 // to avoid having it look like an integer immediate to the assembler.
368 if (Name[0] == '$')
369 O << '(' << Name << ')';
370 else
371 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000372 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000374 }
Chris Lattner797a0782009-06-27 05:46:24 +0000375
376 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000378 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000379 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000380 break;
381 case X86II::MO_DARWIN_NONLAZY:
382 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
383 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000384 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000385 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000386 break;
387 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
388 O << " + [.-";
389 PrintPICBaseSymbol();
390 O << ']';
391 break;
392 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000393 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
394 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000395 O << '-';
396 PrintPICBaseSymbol();
397 break;
398 case X86II::MO_TLSGD: O << "@TLSGD"; break;
399 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
400 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
401 case X86II::MO_TPOFF: O << "@TPOFF"; break;
402 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
403 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
404 case X86II::MO_GOT: O << "@GOT"; break;
405 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000406 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 }
408}
409
Chris Lattnerae8f9592009-07-13 21:53:19 +0000410/// print_pcrel_imm - This is used to print an immediate value that ends up
411/// being encoded as a pc-relative value. These print slightly differently, for
412/// example, a $ is not emitted.
413void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
414 const MachineOperand &MO = MI->getOperand(OpNo);
415 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000416 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000417 case MachineOperand::MO_Immediate:
418 O << MO.getImm();
419 return;
420 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman2aa282f2009-08-13 01:36:44 +0000421 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000422 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000423 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000424 case MachineOperand::MO_ExternalSymbol:
425 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000426 return;
427 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000428}
429
430
Chris Lattner207a0ca2009-07-13 21:41:08 +0000431
432void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
433 const char *Modifier) {
434 const MachineOperand &MO = MI->getOperand(OpNo);
435 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000436 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000437 case MachineOperand::MO_Register: {
438 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
439 "Virtual registers should not make it this far!");
440 O << '%';
441 unsigned Reg = MO.getReg();
442 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersonac9de032009-08-10 22:56:29 +0000443 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000444 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
445 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Chris Lattner207a0ca2009-07-13 21:41:08 +0000446 Reg = getX86SubSuperRegister(Reg, VT);
447 }
448 O << TRI->getAsmName(Reg);
449 return;
450 }
451
452 case MachineOperand::MO_Immediate:
453 O << '$' << MO.getImm();
454 return;
455
456 case MachineOperand::MO_JumpTableIndex:
457 case MachineOperand::MO_ConstantPoolIndex:
458 case MachineOperand::MO_GlobalAddress:
459 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000460 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000461 printSymbolOperand(MO);
462 break;
463 }
464 }
465}
466
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000468 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 assert(value <= 7 && "Invalid ssecc argument!");
470 switch (value) {
471 case 0: O << "eq"; break;
472 case 1: O << "lt"; break;
473 case 2: O << "le"; break;
474 case 3: O << "unord"; break;
475 case 4: O << "neq"; break;
476 case 5: O << "nlt"; break;
477 case 6: O << "nle"; break;
478 case 7: O << "ord"; break;
479 }
480}
481
Rafael Espindolabca99f72009-04-08 21:14:34 +0000482void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000483 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000484 const MachineOperand &BaseReg = MI->getOperand(Op);
485 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 const MachineOperand &DispSpec = MI->getOperand(Op+3);
487
Chris Lattner791fd482009-07-09 00:27:29 +0000488 // If we really don't want to print out (rip), don't.
489 bool HasBaseReg = BaseReg.getReg() != 0;
490 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
491 BaseReg.getReg() == X86::RIP)
492 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000493
494 // HasParenPart - True if we will print out the () part of the mem ref.
495 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
496
497 if (DispSpec.isImm()) {
498 int DispVal = DispSpec.getImm();
499 if (DispVal || !HasParenPart)
500 O << DispVal;
501 } else {
502 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
503 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000504 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000505 }
506
507 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000508 assert(IndexReg.getReg() != X86::ESP &&
509 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000510
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000511 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000512 if (HasBaseReg)
513 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514
515 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000516 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000517 printOperand(MI, Op+2, Modifier);
518 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000520 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000522 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 }
524}
525
Rafael Espindolabca99f72009-04-08 21:14:34 +0000526void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000527 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000528 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000529 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000530 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000531 printOperand(MI, Op+4, Modifier);
532 O << ':';
533 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000534 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000535}
536
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000537void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000538 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000539 if (!MAI->getSetDirective())
Evan Cheng6fb06762007-11-09 01:32:10 +0000540 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000541
542 // We don't need .set machinery if we have GOT-style relocations
543 if (Subtarget->isPICStyleGOT())
544 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000545
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000546 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng6fb06762007-11-09 01:32:10 +0000547 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000548 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000549 if (Subtarget->isPICStyleRIPRel())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000550 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000551 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000552 else {
553 O << '-';
554 PrintPICBaseSymbol();
555 O << '\n';
556 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000557}
558
Chris Lattner14f791a2009-06-24 19:19:16 +0000559
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000561 PrintPICBaseSymbol();
562 O << '\n';
563 PrintPICBaseSymbol();
564 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565}
566
567
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000568void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
569 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000570 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000571 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000572 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000573
574 O << JTEntryDirective << ' ';
575
Chris Lattner2e9393c2009-07-10 21:00:45 +0000576 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000577 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner4a948932009-07-10 20:47:30 +0000578 << '_' << uid << "_set_" << MBB->getNumber();
579 } else if (Subtarget->isPICStyleGOT()) {
580 printBasicBlockLabel(MBB, false, false, false);
581 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000582 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000583 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000584}
585
Chris Lattner8bb96e42009-06-15 04:42:32 +0000586bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 unsigned Reg = MO.getReg();
588 switch (Mode) {
589 default: return true; // Unknown mode.
590 case 'b': // Print QImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000591 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 break;
593 case 'h': // Print QImode high register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000594 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 break;
596 case 'w': // Print HImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000597 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 break;
599 case 'k': // Print SImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000600 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000602 case 'q': // Print DImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000603 Reg = getX86SubSuperRegister(Reg, MVT::i64);
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000604 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 }
606
Evan Cheng00d04a72008-07-07 22:21:06 +0000607 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 return false;
609}
610
611/// PrintAsmOperand - Print out an operand for an inline asm expression.
612///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000613bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000614 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 const char *ExtraCode) {
616 // Does this asm operand have a single letter operand modifier?
617 if (ExtraCode && ExtraCode[0]) {
618 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000619
Chris Lattnerb6047e62009-07-13 21:48:33 +0000620 const MachineOperand &MO = MI->getOperand(OpNo);
621
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 switch (ExtraCode[0]) {
623 default: return true; // Unknown modifier.
Dale Johannesenfabae3e2009-08-19 22:44:41 +0000624 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
625 if (MO.isImm()) {
626 O << MO.getImm();
627 return false;
Dale Johannesen82aca6d2009-08-19 23:44:01 +0000628 }
Dale Johannesenf1f04ec2009-08-25 00:16:14 +0000629 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
630 printSymbolOperand(MO);
631 return false;
632 }
Dale Johannesen82aca6d2009-08-19 23:44:01 +0000633 if (MO.isReg()) {
Dale Johannesenfabae3e2009-08-19 22:44:41 +0000634 O << '(';
635 printOperand(MI, OpNo);
636 O << ')';
637 return false;
638 }
639 return true;
640
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000642 if (MO.isImm())
643 O << MO.getImm();
644 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
645 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000646 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000647 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000649
650 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000651 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000652 O << '*';
653 printOperand(MI, OpNo);
654 return false;
655 }
656 return true;
657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 case 'b': // Print QImode register
659 case 'h': // Print QImode high register
660 case 'w': // Print HImode register
661 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000662 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000663 if (MO.isReg())
664 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 printOperand(MI, OpNo);
666 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000667
Dale Johannesen375b88b2009-07-07 23:28:22 +0000668 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000669 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000671
Chris Lattnerb6047e62009-07-13 21:48:33 +0000672 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000673 // Note: this is a temporary solution. It should be handled target
674 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000675 if (MO.isImm()) {
676 O << -MO.getImm();
677 return false;
678 }
679 O << '-';
680 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000682
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 printOperand(MI, OpNo);
684 return false;
685}
686
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000687bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000689 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000691 if (ExtraCode && ExtraCode[0]) {
692 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000693
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000694 switch (ExtraCode[0]) {
695 default: return true; // Unknown modifier.
696 case 'b': // Print QImode register
697 case 'h': // Print QImode high register
698 case 'w': // Print HImode register
699 case 'k': // Print SImode register
700 case 'q': // Print SImode register
701 // These only apply to registers, ignore on mem.
702 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000703 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000704 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000705 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000706 }
707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 printMemReference(MI, OpNo);
709 return false;
710}
711
Chris Lattnerf5da5902009-06-20 07:03:18 +0000712static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
713 // Convert registers in the addr mode according to subreg64.
714 for (unsigned i = 0; i != 4; ++i) {
715 if (!MI->getOperand(i).isReg()) continue;
716
717 unsigned Reg = MI->getOperand(i).getReg();
718 if (Reg == 0) continue;
719
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000720 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
Chris Lattnerf5da5902009-06-20 07:03:18 +0000721 }
722}
723
Chris Lattner8fffd452009-08-16 03:12:25 +0000724/// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
725/// MCOperand.
726MCOperand X86ATTAsmPrinter::LowerGlobalAddressOperand(const MachineOperand &MO){
Chris Lattner8fffd452009-08-16 03:12:25 +0000727 const GlobalValue *GV = MO.getGlobal();
728
729 const char *Suffix = "";
730 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
731 Suffix = "$stub";
732 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
733 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
734 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
735 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
736 Suffix = "$non_lazy_ptr";
737
738 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
739 if (Subtarget->isTargetCygMing())
740 DecorateCygMingName(Name, GV);
741
742 // Handle dllimport linkage.
743 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
744 Name = "__imp_" + Name;
745
746 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
747 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
748 GVStubs[Name] = Mang->getMangledName(GV);
749 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
750 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
751 HiddenGVStubs[Name] = Mang->getMangledName(GV);
752 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
753 FnStubs[Name] = Mang->getMangledName(GV);
754
Chris Lattner7e5bd8c2009-08-18 05:33:27 +0000755
756 // Handle target operand flags.
757 // FIXME: This should be common between external symbols, constant pool etc.
758 MCSymbol *NegatedSymbol = 0;
759
760 switch (MO.getTargetFlags()) {
761 default:
762 llvm_unreachable("Unknown target flag on GV operand");
763 case X86II::MO_NO_FLAG: // No flag.
764 break;
765 case X86II::MO_DARWIN_NONLAZY:
766 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
767 case X86II::MO_DLLIMPORT:
768 case X86II::MO_DARWIN_STUB:
769 // These affect the name of the symbol, not any suffix.
770 break;
771 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
772 assert(0 && "Reloc mode unimp!");
773 //O << " + [.-";
774 //PrintPICBaseSymbol();
775 //O << ']';
776 break;
777 case X86II::MO_PIC_BASE_OFFSET:
778 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
779 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
780 // Subtract the pic base.
781 NegatedSymbol = GetPICBaseSymbol();
782 break;
783
784 // FIXME: These probably should be a modifier on the symbol or something??
785 case X86II::MO_TLSGD: Name += "@TLSGD"; break;
786 case X86II::MO_GOTTPOFF: Name += "@GOTTPOFF"; break;
787 case X86II::MO_INDNTPOFF: Name += "@INDNTPOFF"; break;
788 case X86II::MO_TPOFF: Name += "@TPOFF"; break;
789 case X86II::MO_NTPOFF: Name += "@NTPOFF"; break;
790 case X86II::MO_GOTPCREL: Name += "@GOTPCREL"; break;
791 case X86II::MO_GOT: Name += "@GOT"; break;
792 case X86II::MO_GOTOFF: Name += "@GOTOFF"; break;
793 case X86II::MO_PLT: Name += "@PLT"; break;
794 }
795
Chris Lattner8fffd452009-08-16 03:12:25 +0000796 // Create a symbol for the name.
797 MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000798 // FIXME: We would like an efficient form for this, so we don't have to do a
799 // lot of extra uniquing.
800 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, OutContext);
801 if (NegatedSymbol)
802 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(NegatedSymbol,
803 OutContext),
804 OutContext);
805 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(MO.getOffset(),
806 OutContext),
807 OutContext);
808 return MCOperand::CreateExpr(Expr);
Chris Lattner8fffd452009-08-16 03:12:25 +0000809}
810
Chris Lattnerf39997f2009-08-16 04:28:14 +0000811MCOperand X86ATTAsmPrinter::
812LowerExternalSymbolOperand(const MachineOperand &MO){
813 std::string Name = Mang->makeNameProper(MO.getSymbolName());
814 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
815 FnStubs[Name+"$stub"] = Name;
816 Name += "$stub";
817 }
818
819 MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000820 // FIXME: We would like an efficient form for this, so we don't have to do a
821 // lot of extra uniquing.
822 const MCExpr *Expr =
823 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, OutContext),
824 MCConstantExpr::Create(MO.getOffset(),OutContext),
825 OutContext);
826 return MCOperand::CreateExpr(Expr);
Chris Lattnerf39997f2009-08-16 04:28:14 +0000827}
828
829
Bill Wendlingb3b11262009-02-06 21:45:08 +0000830/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
831/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832///
833void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
834 ++EmittedInsts;
835
Chris Lattnerf39997f2009-08-16 04:28:14 +0000836 if (!NewAsmPrinter) {
837 // Call the autogenerated instruction printer routines.
838 printInstruction(MI);
839 return;
840 }
841
842 MCInst TmpInst;
843
844 switch (MI->getOpcode()) {
845 case TargetInstrInfo::DBG_LABEL:
846 case TargetInstrInfo::EH_LABEL:
847 case TargetInstrInfo::GC_LABEL:
848 printLabel(MI);
849 return;
850 case TargetInstrInfo::INLINEASM:
851 O << '\t';
852 printInlineAsm(MI);
853 return;
Chris Lattnerf39997f2009-08-16 04:28:14 +0000854 case TargetInstrInfo::IMPLICIT_DEF:
855 printImplicitDef(MI);
856 return;
857 case X86::MOVPC32r: {
858 // This is a pseudo op for a two instruction sequence with a label, which
859 // looks like:
860 // call "L1$pb"
861 // "L1$pb":
862 // popl %esi
Chris Lattnerf5da5902009-06-20 07:03:18 +0000863
Chris Lattnerf39997f2009-08-16 04:28:14 +0000864 // Emit the call.
865 MCSymbol *PICBase = GetPICBaseSymbol();
866 TmpInst.setOpcode(X86::CALLpcrel32);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000867 // FIXME: We would like an efficient form for this, so we don't have to do a
868 // lot of extra uniquing.
869 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase,
870 OutContext)));
Chris Lattnerf39997f2009-08-16 04:28:14 +0000871 printInstruction(&TmpInst);
872
873 // Emit the label.
874 OutStreamer.EmitLabel(PICBase);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000875
Chris Lattnerf39997f2009-08-16 04:28:14 +0000876 // popl $reg
877 TmpInst.setOpcode(X86::POP32r);
878 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
879 printInstruction(&TmpInst);
880 O << "OLD: ";
881 // Call the autogenerated instruction printer routines.
882 printInstruction(MI);
883 return;
884 }
885 }
886
887 O << "NEW: ";
888
889 TmpInst.setOpcode(MI->getOpcode());
890
891 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
892 const MachineOperand &MO = MI->getOperand(i);
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000893
Chris Lattnerf39997f2009-08-16 04:28:14 +0000894 MCOperand MCOp;
895 switch (MO.getType()) {
896 default:
897 O.flush();
898 errs() << "Cannot lower operand #" << i << " of :" << *MI;
899 llvm_unreachable("Unimp");
900 case MachineOperand::MO_Register:
901 MCOp = MCOperand::CreateReg(MO.getReg());
902 break;
903 case MachineOperand::MO_Immediate:
904 MCOp = MCOperand::CreateImm(MO.getImm());
905 break;
906 case MachineOperand::MO_MachineBasicBlock:
907 MCOp = MCOperand::CreateMBBLabel(getFunctionNumber(),
908 MO.getMBB()->getNumber());
909 break;
910 case MachineOperand::MO_GlobalAddress:
911 MCOp = LowerGlobalAddressOperand(MO);
912 break;
913 case MachineOperand::MO_ExternalSymbol:
914 MCOp = LowerExternalSymbolOperand(MO);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000915 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000916 }
917
Chris Lattnerf39997f2009-08-16 04:28:14 +0000918 TmpInst.addOperand(MCOp);
Chris Lattner19b1bd52009-06-19 00:47:33 +0000919 }
920
Chris Lattnerf39997f2009-08-16 04:28:14 +0000921 switch (TmpInst.getOpcode()) {
922 case X86::LEA64_32r:
923 // Handle the 'subreg rewriting' for the lea64_32mem operand.
924 lower_lea64_32mem(&TmpInst, 1);
925 break;
926 }
927
928 // FIXME: Convert TmpInst.
929 printInstruction(&TmpInst);
930 O << "OLD: ";
931
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 // Call the autogenerated instruction printer routines.
933 printInstruction(MI);
934}
935
Chris Lattnerae982212009-07-21 18:38:57 +0000936void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000937 const TargetData *TD = TM.getTargetData();
938
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000939 if (!GVar->hasInitializer())
940 return; // External global require no code
941
942 // Check to see if this is a special global used by LLVM, if so, emit it.
943 if (EmitSpecialLLVMGlobal(GVar)) {
944 if (Subtarget->isTargetDarwin() &&
945 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000946 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000947 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000948 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000949 O << ".reference .destructors_used\n";
950 }
951 return;
952 }
953
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000954 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000955 Constant *C = GVar->getInitializer();
956 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000957 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000958 unsigned Align = TD->getPreferredAlignmentLog(GVar);
959
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000960 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000961
962 if (Subtarget->isTargetELF())
963 O << "\t.type\t" << name << ",@object\n";
964
Chris Lattner7215c7f2009-08-05 05:21:07 +0000965
966 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GVar, TM);
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000967 const MCSection *TheSection =
Chris Lattner7215c7f2009-08-05 05:21:07 +0000968 getObjFileLowering().SectionForGlobal(GVar, GVKind, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000969 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000970
Chris Lattnerdb727932009-08-04 05:35:56 +0000971 // FIXME: get this stuff from section kind flags.
Evan Chengcf84b142009-02-18 02:19:52 +0000972 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000973 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000974 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000975 if (GVar->hasExternalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000976 if (const char *Directive = MAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000977 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000978 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000979 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000980 return;
981 }
982 }
983
984 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000985 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000986 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000987
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000988 if (MAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000989 if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000990 O << MAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000991 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000992 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000993 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000994 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000995 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000996 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000997 O << name << ":";
998 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000999 O.PadToColumn(MAI->getCommentColumn());
1000 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +00001001 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001002 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001003 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001004 EmitGlobalConstant(C);
1005 return;
1006 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001007 O << MAI->getCOMMDirective() << name << ',' << Size;
1008 if (MAI->getCOMMDirectiveTakesAlignment())
1009 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001010 }
1011 } else {
1012 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001013 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001014 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001015 }
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001016 O << MAI->getCOMMDirective() << name << ',' << Size;
1017 if (MAI->getCOMMDirectiveTakesAlignment())
1018 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001019 }
Evan Cheng11db8142009-03-24 00:17:40 +00001020 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001021 O.PadToColumn(MAI->getCommentColumn());
1022 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +00001023 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001024 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001025 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001026 return;
1027 }
1028 }
1029
1030 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +00001031 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +00001032 case GlobalValue::LinkOnceAnyLinkage:
1033 case GlobalValue::LinkOnceODRLinkage:
1034 case GlobalValue::WeakAnyLinkage:
1035 case GlobalValue::WeakODRLinkage:
Dale Johannesenf03574c2009-08-13 00:28:52 +00001036 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001037 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001038 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001039 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001040 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001041 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001042 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001043 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001044 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001045 }
1046 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001047 case GlobalValue::DLLExportLinkage:
1048 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001049 // FIXME: appending linkage variables should go into a section of
1050 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +00001051 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001052 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001053 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001054 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001055 case GlobalValue::PrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +00001056 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001057 break;
Evan Cheng630c5612008-08-08 06:43:59 +00001058 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001059 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001060 }
1061
1062 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001063 O << name << ":";
1064 if (VerboseAsm){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001065 O.PadToColumn(MAI->getCommentColumn());
1066 O << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +00001067 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001068 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001069 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001070
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001071 EmitGlobalConstant(C);
Dan Gohman2aa282f2009-08-13 01:36:44 +00001072
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001073 if (MAI->hasDotTypeDotSizeDirective())
Dan Gohman2aa282f2009-08-13 01:36:44 +00001074 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001075}
1076
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +00001077bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001078 // Print out module-level global variables here.
1079 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001080 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +00001081 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001082 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +00001083 }
1084
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001085 if (Subtarget->isTargetDarwin()) {
Chris Lattner92873462009-08-03 21:53:27 +00001086 // All darwin targets use mach-o.
1087 TargetLoweringObjectFileMachO &TLOFMacho =
1088 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2e860262009-06-24 18:24:09 +00001089
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001090 // Add the (possibly multiple) personalities to the set of global value
1091 // stubs. Only referenced functions get into the Personalities list.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001092 if (MAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +00001093 const std::vector<Function*> &Personalities = MMI->getPersonalities();
1094 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001095 if (Personalities[i])
1096 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
1097 true /*private label*/)] =
1098 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +00001099 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001100 }
1101
Chris Lattner2e860262009-06-24 18:24:09 +00001102 // Output stubs for dynamically-linked functions
1103 if (!FnStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +00001104 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001105 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
1106 MCSectionMachO::S_SYMBOL_STUBS |
1107 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
1108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1109 5, SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +00001110 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001111 for (StringMap<std::string>::iterator I = FnStubs.begin(),
1112 E = FnStubs.end(); I != E; ++I)
1113 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
1114 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +00001115 O << '\n';
1116 }
1117
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001118 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +00001119 if (!GVStubs.empty()) {
Chris Lattner92873462009-08-03 21:53:27 +00001120 const MCSection *TheSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001121 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
1122 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner92873462009-08-03 21:53:27 +00001123 SectionKind::getMetadata());
Chris Lattner73266f92009-08-19 05:49:37 +00001124 OutStreamer.SwitchSection(TheSection);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001125 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1126 E = GVStubs.end(); I != E; ++I)
1127 O << I->getKeyData() << ":\n\t.indirect_symbol "
1128 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +00001129 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001130
Evan Chenga65854f2008-12-05 01:06:39 +00001131 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001132 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +00001133 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001134 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1135 E = HiddenGVStubs.end(); I != E; ++I)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001136 O << I->getKeyData() << ":\n" << MAI->getData32bitsDirective()
Chris Lattnerb3cdde62009-07-14 18:17:16 +00001137 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001138 }
1139
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001140 // Funny Darwin hack: This flag tells the linker that no global symbols
1141 // contain code that falls through to other global symbols (e.g. the obvious
1142 // implementation of multiple entry points). If this doesn't occur, the
1143 // linker can safely perform dead code stripping. Since LLVM never
1144 // generates code that does this, it is always safe to set.
1145 O << "\t.subsections_via_symbols\n";
1146 } else if (Subtarget->isTargetCygMing()) {
1147 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +00001148 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001149 i != e; ++i) {
1150 O << "\t.def\t " << i->getKeyData()
1151 << ";\t.scl\t" << COFF::C_EXT
1152 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1153 << ";\t.endef\n";
1154 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001155 }
Chris Lattner5ec2b662009-06-24 05:47:59 +00001156
Chris Lattnerdb191f02009-06-24 18:52:01 +00001157
1158 // Output linker support code for dllexported globals on windows.
Chris Lattner92873462009-08-03 21:53:27 +00001159 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
1160 // dllexport symbols only exist on coff targets.
1161 TargetLoweringObjectFileCOFF &TLOFMacho =
1162 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
1163
Chris Lattner73266f92009-08-19 05:49:37 +00001164 OutStreamer.SwitchSection(TLOFMacho.getCOFFSection(".section .drectve",true,
1165 SectionKind::getMetadata()));
Chris Lattnerdb191f02009-06-24 18:52:01 +00001166
1167 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1168 e = DLLExportedGVs.end(); i != e; ++i)
1169 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Chris Lattnerdb191f02009-06-24 18:52:01 +00001170
1171 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1172 e = DLLExportedFns.end();
1173 i != e; ++i)
1174 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1175 }
1176
Chris Lattnerdb191f02009-06-24 18:52:01 +00001177 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +00001178 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001179}
1180
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001181// Include the auto-generated portion of the assembly writer.
1182#include "X86GenAsmWriter.inc"