blob: 9b73e87cc676f90515a220ab5575e5c4dbff069e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Devang Patel7c368852009-07-28 21:49:47 +000026#include "llvm/Metadata.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000027#include "llvm/Type.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000030#include "llvm/MC/MCContext.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000031#include "llvm/MC/MCInst.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000032#include "llvm/MC/MCSection.h"
Chris Lattner7cf8daf2009-06-24 05:46:28 +000033#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000034#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000036#include "llvm/Support/CommandLine.h"
Edwin Török675d5622009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000038#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Support/Mangler.h"
40#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043using namespace llvm;
44
45STATISTIC(EmittedInsts, "Number of machine instrs printed");
46
Chris Lattner19b1bd52009-06-19 00:47:33 +000047static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
48 cl::Hidden);
49
Chris Lattner2e845952009-06-24 19:44:36 +000050//===----------------------------------------------------------------------===//
51// Primitive Helper Functions.
52//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000053
54void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000056 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 else if (Subtarget->isTargetELF())
Chris Lattner021ad452009-07-08 23:09:14 +000058 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 else
Edwin Törökbd448e32009-07-14 16:55:14 +000060 llvm_unreachable("Don't know how to print PIC label!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061}
62
Chris Lattner2e845952009-06-24 19:44:36 +000063/// PrintUnmangledNameSafely - Print out the printable characters in the name.
64/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +000065static void PrintUnmangledNameSafely(const Value *V,
David Greene302008d2009-07-14 20:18:05 +000066 formatted_raw_ostream &OS) {
Daniel Dunbar23e2b802009-07-26 07:49:05 +000067 for (StringRef::iterator it = V->getName().begin(),
68 ie = V->getName().end(); it != ie; ++it)
69 if (isprint(*it))
70 OS << *it;
Chris Lattner2e845952009-06-24 19:44:36 +000071}
Chris Lattner14f791a2009-06-24 19:19:16 +000072
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000073static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
74 const TargetData *TD) {
75 X86MachineFunctionInfo Info;
76 uint64_t Size = 0;
77
78 switch (F->getCallingConv()) {
79 case CallingConv::X86_StdCall:
80 Info.setDecorationStyle(StdCall);
81 break;
82 case CallingConv::X86_FastCall:
83 Info.setDecorationStyle(FastCall);
84 break;
85 default:
86 return Info;
87 }
88
89 unsigned argNum = 1;
90 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
91 AI != AE; ++AI, ++argNum) {
92 const Type* Ty = AI->getType();
93
94 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000095 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000096 Ty = cast<PointerType>(Ty)->getElementType();
97
98 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000099 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000100 }
101
102 // We're not supporting tooooo huge arguments :)
103 Info.setBytesToPopOnReturn((unsigned int)Size);
104 return Info;
105}
106
Chris Lattner8cfe9142009-07-15 04:55:56 +0000107/// DecorateCygMingName - Query FunctionInfoMap and use this information for
108/// various name decorations for Cygwin and MingW.
109void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
110 const GlobalValue *GV) {
111 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
112
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000113 const Function *F = dyn_cast<Function>(GV);
114 if (!F) return;
115
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000116 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000117 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000118 CygMingStubs.insert(Name);
119
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000120 // We don't want to decorate non-stdcall or non-fastcall functions right now
121 unsigned CC = F->getCallingConv();
122 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
123 return;
124
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000125
126 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000127
128 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000129 if (info_item == FunctionInfoMap.end()) {
130 // Calculate apropriate function info and populate map
131 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
132 Info = &FunctionInfoMap[F];
133 } else {
134 Info = &info_item->second;
135 }
136
137 const FunctionType *FT = F->getFunctionType();
138 switch (Info->getDecorationStyle()) {
139 case None:
140 break;
141 case StdCall:
142 // "Pure" variadic functions do not receive @0 suffix.
143 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
144 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
145 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
146 break;
147 case FastCall:
148 // "Pure" variadic functions do not receive @0 suffix.
149 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
150 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
151 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
152
153 if (Name[0] == '_') {
154 Name[0] = '@';
155 } else {
156 Name = '@' + Name;
157 }
158 break;
159 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000160 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000161 }
162}
163
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000164void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000165 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167
Chris Lattner8cfe9142009-07-15 04:55:56 +0000168 if (Subtarget->isTargetCygMing())
169 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170
Chris Lattner2931fe42009-07-29 05:09:30 +0000171 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000173 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000175 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000176 case Function::LinkerPrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000177 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 break;
179 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000181 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000182 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000184 case Function::LinkOnceAnyLinkage:
185 case Function::LinkOnceODRLinkage:
186 case Function::WeakAnyLinkage:
187 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000188 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000190 O << "\t.globl\t" << CurrentFnName << '\n';
191 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000193 O << "\t.globl\t" << CurrentFnName << "\n"
194 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000196 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 }
198 break;
199 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000200
201 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202
203 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000204 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 else if (Subtarget->isTargetCygMing()) {
206 O << "\t.def\t " << CurrentFnName
207 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000208 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
210 << ";\t.endef\n";
211 }
212
213 O << CurrentFnName << ":\n";
214 // Add some workaround for linkonce linkage on Cygwin\MinGW
215 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000216 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000218}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000220/// runOnMachineFunction - This uses the printMachineInstruction()
221/// method to print assembly for each instruction.
222///
223bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
224 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000225 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000226 unsigned CC = F->getCallingConv();
227
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000228 SetupMachineFunction(MF);
229 O << "\n\n";
230
231 // Populate function information map. Actually, We don't want to populate
232 // non-stdcall or non-fastcall functions' information right now.
233 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
234 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
235
236 // Print out constants referenced by the function
237 EmitConstantPool(MF.getConstantPool());
238
239 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000240 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000241
242 // Print the 'header' of function
243 emitFunctionHeader(MF);
244
245 // Emit pre-function debug and/or EH information.
246 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000247 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000250 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
252 I != E; ++I) {
253 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000254 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
255 // This is an entry block or a block that's only reachable via a
256 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
257 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000258 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 O << '\n';
260 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000261 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
262 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000264 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000265 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 printMachineInstruction(II);
267 }
268 }
269
Dale Johannesenf35771f2008-04-08 00:37:56 +0000270 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
271 // If the function is empty, then we need to emit *something*. Otherwise,
272 // the function's label might be associated with something that it wasn't
273 // meant to be associated with. We emit a noop in this situation.
274 // We are assuming inline asms are code.
275 O << "\tnop\n";
276 }
277
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000279 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000281 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000282 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000283 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284
285 // Print out jump tables referenced by the function.
286 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000287
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000288 O.flush();
289
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 // We didn't modify anything.
291 return false;
292}
293
Chris Lattnerae8f9592009-07-13 21:53:19 +0000294/// printSymbolOperand - Print a raw symbol reference operand. This handles
295/// jump tables, constant pools, global address and external symbols, all of
296/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000297void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
298 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000299 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000300 case MachineOperand::MO_JumpTableIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000301 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000302 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000303 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000304 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000305 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000306 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000307 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000308 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000310 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000311
312 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000313 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
314 Suffix = "$stub";
315 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
316 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
317 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000318 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000319 Suffix = "$non_lazy_ptr";
320
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000321 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000322 if (Subtarget->isTargetCygMing())
323 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000324
Chris Lattner0cc2b792009-07-09 05:42:07 +0000325 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000326 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
327 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000328
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000329 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
330 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
331 GVStubs[Name] = Mang->getMangledName(GV);
332 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
333 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
334 HiddenGVStubs[Name] = Mang->getMangledName(GV);
335 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
336 FnStubs[Name] = Mang->getMangledName(GV);
337
338 // If the name begins with a dollar-sign, enclose it in parens. We do this
339 // to avoid having it look like an integer immediate to the assembler.
340 if (Name[0] == '$')
341 O << '(' << Name << ')';
342 else
343 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000344
Chris Lattner0cc2b792009-07-09 05:42:07 +0000345 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000346 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000348 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000349 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000350 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000351 FnStubs[Name+"$stub"] = Name;
352 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000353 }
354
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000355 // If the name begins with a dollar-sign, enclose it in parens. We do this
356 // to avoid having it look like an integer immediate to the assembler.
357 if (Name[0] == '$')
358 O << '(' << Name << ')';
359 else
360 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000361 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000363 }
Chris Lattner797a0782009-06-27 05:46:24 +0000364
365 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000367 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000368 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000369 break;
370 case X86II::MO_DARWIN_NONLAZY:
371 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
372 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000373 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000374 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000375 break;
376 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
377 O << " + [.-";
378 PrintPICBaseSymbol();
379 O << ']';
380 break;
381 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000382 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
383 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000384 O << '-';
385 PrintPICBaseSymbol();
386 break;
387 case X86II::MO_TLSGD: O << "@TLSGD"; break;
388 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
389 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
390 case X86II::MO_TPOFF: O << "@TPOFF"; break;
391 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
392 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
393 case X86II::MO_GOT: O << "@GOT"; break;
394 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000395 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 }
397}
398
Chris Lattnerae8f9592009-07-13 21:53:19 +0000399/// print_pcrel_imm - This is used to print an immediate value that ends up
400/// being encoded as a pc-relative value. These print slightly differently, for
401/// example, a $ is not emitted.
402void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
403 const MachineOperand &MO = MI->getOperand(OpNo);
404 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000405 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000406 case MachineOperand::MO_Immediate:
407 O << MO.getImm();
408 return;
409 case MachineOperand::MO_MachineBasicBlock:
410 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
411 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000412 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000413 case MachineOperand::MO_ExternalSymbol:
414 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000415 return;
416 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000417}
418
419
Chris Lattner207a0ca2009-07-13 21:41:08 +0000420
421void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
422 const char *Modifier) {
423 const MachineOperand &MO = MI->getOperand(OpNo);
424 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000425 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000426 case MachineOperand::MO_Register: {
427 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
428 "Virtual registers should not make it this far!");
429 O << '%';
430 unsigned Reg = MO.getReg();
431 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
432 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
433 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
434 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
435 Reg = getX86SubSuperRegister(Reg, VT);
436 }
437 O << TRI->getAsmName(Reg);
438 return;
439 }
440
441 case MachineOperand::MO_Immediate:
442 O << '$' << MO.getImm();
443 return;
444
445 case MachineOperand::MO_JumpTableIndex:
446 case MachineOperand::MO_ConstantPoolIndex:
447 case MachineOperand::MO_GlobalAddress:
448 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000449 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000450 printSymbolOperand(MO);
451 break;
452 }
453 }
454}
455
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000457 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 assert(value <= 7 && "Invalid ssecc argument!");
459 switch (value) {
460 case 0: O << "eq"; break;
461 case 1: O << "lt"; break;
462 case 2: O << "le"; break;
463 case 3: O << "unord"; break;
464 case 4: O << "neq"; break;
465 case 5: O << "nlt"; break;
466 case 6: O << "nle"; break;
467 case 7: O << "ord"; break;
468 }
469}
470
Rafael Espindolabca99f72009-04-08 21:14:34 +0000471void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000472 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000473 const MachineOperand &BaseReg = MI->getOperand(Op);
474 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 const MachineOperand &DispSpec = MI->getOperand(Op+3);
476
Chris Lattner791fd482009-07-09 00:27:29 +0000477 // If we really don't want to print out (rip), don't.
478 bool HasBaseReg = BaseReg.getReg() != 0;
479 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
480 BaseReg.getReg() == X86::RIP)
481 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000482
483 // HasParenPart - True if we will print out the () part of the mem ref.
484 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
485
486 if (DispSpec.isImm()) {
487 int DispVal = DispSpec.getImm();
488 if (DispVal || !HasParenPart)
489 O << DispVal;
490 } else {
491 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
492 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000493 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000494 }
495
496 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000497 assert(IndexReg.getReg() != X86::ESP &&
498 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000499
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000500 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000501 if (HasBaseReg)
502 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503
504 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000505 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000506 printOperand(MI, Op+2, Modifier);
507 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000509 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000511 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 }
513}
514
Rafael Espindolabca99f72009-04-08 21:14:34 +0000515void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000516 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000517 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000518 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000519 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000520 printOperand(MI, Op+4, Modifier);
521 O << ':';
522 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000523 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000524}
525
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000526void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000527 const MachineBasicBlock *MBB) const {
528 if (!TAI->getSetDirective())
529 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000530
531 // We don't need .set machinery if we have GOT-style relocations
532 if (Subtarget->isPICStyleGOT())
533 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000534
Evan Cheng6fb06762007-11-09 01:32:10 +0000535 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
536 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000537 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000538 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000539 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000540 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000541 else {
542 O << '-';
543 PrintPICBaseSymbol();
544 O << '\n';
545 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000546}
547
Chris Lattner14f791a2009-06-24 19:19:16 +0000548
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000550 PrintPICBaseSymbol();
551 O << '\n';
552 PrintPICBaseSymbol();
553 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554}
555
556
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000557void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
558 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000559 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000560 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
561 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
562
563 O << JTEntryDirective << ' ';
564
Chris Lattner2e9393c2009-07-10 21:00:45 +0000565 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000566 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
567 << '_' << uid << "_set_" << MBB->getNumber();
568 } else if (Subtarget->isPICStyleGOT()) {
569 printBasicBlockLabel(MBB, false, false, false);
570 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000571 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000572 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000573}
574
Chris Lattner8bb96e42009-06-15 04:42:32 +0000575bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 unsigned Reg = MO.getReg();
577 switch (Mode) {
578 default: return true; // Unknown mode.
579 case 'b': // Print QImode register
580 Reg = getX86SubSuperRegister(Reg, MVT::i8);
581 break;
582 case 'h': // Print QImode high register
583 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
584 break;
585 case 'w': // Print HImode register
586 Reg = getX86SubSuperRegister(Reg, MVT::i16);
587 break;
588 case 'k': // Print SImode register
589 Reg = getX86SubSuperRegister(Reg, MVT::i32);
590 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000591 case 'q': // Print DImode register
592 Reg = getX86SubSuperRegister(Reg, MVT::i64);
593 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 }
595
Evan Cheng00d04a72008-07-07 22:21:06 +0000596 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 return false;
598}
599
600/// PrintAsmOperand - Print out an operand for an inline asm expression.
601///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000602bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000603 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 const char *ExtraCode) {
605 // Does this asm operand have a single letter operand modifier?
606 if (ExtraCode && ExtraCode[0]) {
607 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000608
Chris Lattnerb6047e62009-07-13 21:48:33 +0000609 const MachineOperand &MO = MI->getOperand(OpNo);
610
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 switch (ExtraCode[0]) {
612 default: return true; // Unknown modifier.
613 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000614 if (MO.isImm())
615 O << MO.getImm();
616 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
617 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000618 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000619 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000621
622 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000623 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000624 O << '*';
625 printOperand(MI, OpNo);
626 return false;
627 }
628 return true;
629
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 case 'b': // Print QImode register
631 case 'h': // Print QImode high register
632 case 'w': // Print HImode register
633 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000634 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000635 if (MO.isReg())
636 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 printOperand(MI, OpNo);
638 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000639
Dale Johannesen375b88b2009-07-07 23:28:22 +0000640 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000641 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000643
Chris Lattnerb6047e62009-07-13 21:48:33 +0000644 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000645 // Note: this is a temporary solution. It should be handled target
646 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000647 if (MO.isImm()) {
648 O << -MO.getImm();
649 return false;
650 }
651 O << '-';
652 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000654
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 printOperand(MI, OpNo);
656 return false;
657}
658
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000659bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000661 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000663 if (ExtraCode && ExtraCode[0]) {
664 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000665
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000666 switch (ExtraCode[0]) {
667 default: return true; // Unknown modifier.
668 case 'b': // Print QImode register
669 case 'h': // Print QImode high register
670 case 'w': // Print HImode register
671 case 'k': // Print SImode register
672 case 'q': // Print SImode register
673 // These only apply to registers, ignore on mem.
674 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000675 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000676 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000677 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000678 }
679 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 printMemReference(MI, OpNo);
681 return false;
682}
683
Chris Lattnerf5da5902009-06-20 07:03:18 +0000684static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
685 // Convert registers in the addr mode according to subreg64.
686 for (unsigned i = 0; i != 4; ++i) {
687 if (!MI->getOperand(i).isReg()) continue;
688
689 unsigned Reg = MI->getOperand(i).getReg();
690 if (Reg == 0) continue;
691
692 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
693 }
694}
695
Bill Wendlingb3b11262009-02-06 21:45:08 +0000696/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
697/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698///
699void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
700 ++EmittedInsts;
701
Chris Lattner19b1bd52009-06-19 00:47:33 +0000702 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000703 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
704 O << "\t";
705 printInlineAsm(MI);
706 return;
707 } else if (MI->isLabel()) {
708 printLabel(MI);
709 return;
710 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
711 printDeclare(MI);
712 return;
713 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
714 printImplicitDef(MI);
715 return;
716 }
717
Chris Lattnere67184e2009-06-19 23:59:57 +0000718 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000719 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000720
721 TmpInst.setOpcode(MI->getOpcode());
722
723 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
724 const MachineOperand &MO = MI->getOperand(i);
725
726 MCOperand MCOp;
727 if (MO.isReg()) {
Daniel Dunbar511f4492009-08-02 00:09:22 +0000728 MCOp = MCOperand::CreateReg(MO.getReg());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000729 } else if (MO.isImm()) {
Daniel Dunbar511f4492009-08-02 00:09:22 +0000730 MCOp = MCOperand::CreateImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000731 } else if (MO.isMBB()) {
Daniel Dunbar511f4492009-08-02 00:09:22 +0000732 MCOp = MCOperand::CreateMBBLabel(getFunctionNumber(),
733 MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000734 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000735 llvm_unreachable("Unimp");
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000736 }
737
738 TmpInst.addOperand(MCOp);
739 }
740
Chris Lattner6b9f7742009-06-20 07:59:10 +0000741 switch (TmpInst.getOpcode()) {
742 case X86::LEA64_32r:
743 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000744 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000745 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000746 }
747
Chris Lattner19b1bd52009-06-19 00:47:33 +0000748 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000749 printInstruction(&TmpInst);
750 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000751 }
752
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 // Call the autogenerated instruction printer routines.
754 printInstruction(MI);
755}
756
Chris Lattnerae982212009-07-21 18:38:57 +0000757void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000758 const TargetData *TD = TM.getTargetData();
759
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000760 if (!GVar->hasInitializer())
761 return; // External global require no code
762
763 // Check to see if this is a special global used by LLVM, if so, emit it.
764 if (EmitSpecialLLVMGlobal(GVar)) {
765 if (Subtarget->isTargetDarwin() &&
766 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000767 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000768 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000769 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000770 O << ".reference .destructors_used\n";
771 }
772 return;
773 }
774
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000775 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000776 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000777 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000778 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000779 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000780 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000781 unsigned Align = TD->getPreferredAlignmentLog(GVar);
782
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000783 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000784
785 if (Subtarget->isTargetELF())
786 O << "\t.type\t" << name << ",@object\n";
787
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000788 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000789 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000790 SwitchToSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000791
Evan Chengcf84b142009-02-18 02:19:52 +0000792 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000793 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000794 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 if (GVar->hasExternalLinkage()) {
796 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000797 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000798 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000799 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000800 return;
801 }
802 }
803
804 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000805 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000806 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000807
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000809 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000810 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000811 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 O << "\t.globl " << name << '\n'
815 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000816 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000817 O << name << ":";
818 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000819 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000820 PrintUnmangledNameSafely(GVar, O);
821 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000822 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000823 EmitGlobalConstant(C);
824 return;
825 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000826 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000827 if (TAI->getCOMMDirectiveTakesAlignment())
828 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000829 }
830 } else {
831 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000832 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000836 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000837 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 }
Evan Cheng11db8142009-03-24 00:17:40 +0000839 if (VerboseAsm) {
840 O << "\t\t" << TAI->getCommentString() << ' ';
841 PrintUnmangledNameSafely(GVar, O);
842 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000843 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000844 return;
845 }
846 }
847
848 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000849 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000850 case GlobalValue::LinkOnceAnyLinkage:
851 case GlobalValue::LinkOnceODRLinkage:
852 case GlobalValue::WeakAnyLinkage:
853 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000855 O << "\t.globl " << name << '\n'
856 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000858 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000859 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000860 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000861 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000862 }
863 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000864 case GlobalValue::DLLExportLinkage:
865 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 // FIXME: appending linkage variables should go into a section of
867 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000868 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000870 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000871 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000872 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000873 case GlobalValue::LinkerPrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000874 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000875 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000876 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000877 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000878 }
879
880 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000881 O << name << ":";
882 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000883 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000884 PrintUnmangledNameSafely(GVar, O);
885 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000886 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000887 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000888 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000889
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000890 EmitGlobalConstant(C);
891}
892
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000893bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000894 // Print out module-level global variables here.
895 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000896 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000897 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000898 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000899 }
900
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000901 if (Subtarget->isTargetDarwin()) {
902 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000903
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000904 // Add the (possibly multiple) personalities to the set of global value
905 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000906 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000907 const std::vector<Function*> &Personalities = MMI->getPersonalities();
908 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000909 if (Personalities[i])
910 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
911 true /*private label*/)] =
912 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000913 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000914 }
915
Chris Lattner2e860262009-06-24 18:24:09 +0000916 // Output stubs for dynamically-linked functions
917 if (!FnStubs.empty()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000918 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
919 "self_modifying_code+pure_instructions,5", 0);
920 for (StringMap<std::string>::iterator I = FnStubs.begin(),
921 E = FnStubs.end(); I != E; ++I)
922 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
923 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000924 O << '\n';
925 }
926
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000927 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000928 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000929 SwitchToDataSection(
930 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000931 for (StringMap<std::string>::iterator I = GVStubs.begin(),
932 E = GVStubs.end(); I != E; ++I)
933 O << I->getKeyData() << ":\n\t.indirect_symbol "
934 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000935 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000936
Evan Chenga65854f2008-12-05 01:06:39 +0000937 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000938 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000939 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000940 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
941 E = HiddenGVStubs.end(); I != E; ++I)
942 O << I->getKeyData() << ":\n" << TAI->getData32bitsDirective()
943 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000944 }
945
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000946 // Funny Darwin hack: This flag tells the linker that no global symbols
947 // contain code that falls through to other global symbols (e.g. the obvious
948 // implementation of multiple entry points). If this doesn't occur, the
949 // linker can safely perform dead code stripping. Since LLVM never
950 // generates code that does this, it is always safe to set.
951 O << "\t.subsections_via_symbols\n";
952 } else if (Subtarget->isTargetCygMing()) {
953 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +0000954 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000955 i != e; ++i) {
956 O << "\t.def\t " << i->getKeyData()
957 << ";\t.scl\t" << COFF::C_EXT
958 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
959 << ";\t.endef\n";
960 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000961 }
Chris Lattner5ec2b662009-06-24 05:47:59 +0000962
Chris Lattnerdb191f02009-06-24 18:52:01 +0000963
964 // Output linker support code for dllexported globals on windows.
965 if (!DLLExportedGVs.empty()) {
966 SwitchToDataSection(".section .drectve");
967
968 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
969 e = DLLExportedGVs.end(); i != e; ++i)
970 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
971 }
972
973 if (!DLLExportedFns.empty()) {
974 SwitchToDataSection(".section .drectve");
975
976 for (StringSet<>::iterator i = DLLExportedFns.begin(),
977 e = DLLExportedFns.end();
978 i != e; ++i)
979 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
980 }
981
Chris Lattnerdb191f02009-06-24 18:52:01 +0000982 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000983 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000984}
985
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986// Include the auto-generated portion of the assembly writer.
987#include "X86GenAsmWriter.inc"