blob: 4b6fe36d93ee97986749fd07b8f5d0d2b8beb9e5 [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 Lattner7cf8daf2009-06-24 05:46:28 +000032#include "llvm/MC/MCStreamer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000033#include "llvm/CodeGen/DwarfWriter.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner19b1bd52009-06-19 00:47:33 +000035#include "llvm/Support/CommandLine.h"
Edwin Török675d5622009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000037#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Support/Mangler.h"
39#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000040#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
44STATISTIC(EmittedInsts, "Number of machine instrs printed");
45
Chris Lattner19b1bd52009-06-19 00:47:33 +000046static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
47 cl::Hidden);
48
Chris Lattner2e845952009-06-24 19:44:36 +000049//===----------------------------------------------------------------------===//
50// Primitive Helper Functions.
51//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000052
53void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000055 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 else if (Subtarget->isTargetELF())
Chris Lattner021ad452009-07-08 23:09:14 +000057 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 else
Edwin Törökbd448e32009-07-14 16:55:14 +000059 llvm_unreachable("Don't know how to print PIC label!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060}
61
Chris Lattner2e845952009-06-24 19:44:36 +000062/// PrintUnmangledNameSafely - Print out the printable characters in the name.
63/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +000064static void PrintUnmangledNameSafely(const Value *V,
David Greene302008d2009-07-14 20:18:05 +000065 formatted_raw_ostream &OS) {
Daniel Dunbar23e2b802009-07-26 07:49:05 +000066 for (StringRef::iterator it = V->getName().begin(),
67 ie = V->getName().end(); it != ie; ++it)
68 if (isprint(*it))
69 OS << *it;
Chris Lattner2e845952009-06-24 19:44:36 +000070}
Chris Lattner14f791a2009-06-24 19:19:16 +000071
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000072static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
73 const TargetData *TD) {
74 X86MachineFunctionInfo Info;
75 uint64_t Size = 0;
76
77 switch (F->getCallingConv()) {
78 case CallingConv::X86_StdCall:
79 Info.setDecorationStyle(StdCall);
80 break;
81 case CallingConv::X86_FastCall:
82 Info.setDecorationStyle(FastCall);
83 break;
84 default:
85 return Info;
86 }
87
88 unsigned argNum = 1;
89 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
90 AI != AE; ++AI, ++argNum) {
91 const Type* Ty = AI->getType();
92
93 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000094 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000095 Ty = cast<PointerType>(Ty)->getElementType();
96
97 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000098 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000099 }
100
101 // We're not supporting tooooo huge arguments :)
102 Info.setBytesToPopOnReturn((unsigned int)Size);
103 return Info;
104}
105
Chris Lattner8cfe9142009-07-15 04:55:56 +0000106/// DecorateCygMingName - Query FunctionInfoMap and use this information for
107/// various name decorations for Cygwin and MingW.
108void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
109 const GlobalValue *GV) {
110 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
111
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000112 const Function *F = dyn_cast<Function>(GV);
113 if (!F) return;
114
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000115 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000116 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000117 CygMingStubs.insert(Name);
118
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000119 // We don't want to decorate non-stdcall or non-fastcall functions right now
120 unsigned CC = F->getCallingConv();
121 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
122 return;
123
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000124
125 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000126
127 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000128 if (info_item == FunctionInfoMap.end()) {
129 // Calculate apropriate function info and populate map
130 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
131 Info = &FunctionInfoMap[F];
132 } else {
133 Info = &info_item->second;
134 }
135
136 const FunctionType *FT = F->getFunctionType();
137 switch (Info->getDecorationStyle()) {
138 case None:
139 break;
140 case StdCall:
141 // "Pure" variadic functions do not receive @0 suffix.
142 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
143 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
144 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
145 break;
146 case FastCall:
147 // "Pure" variadic functions do not receive @0 suffix.
148 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
149 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
150 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
151
152 if (Name[0] == '_') {
153 Name[0] = '@';
154 } else {
155 Name = '@' + Name;
156 }
157 break;
158 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000159 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000160 }
161}
162
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000163void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000164 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166
Chris Lattner8cfe9142009-07-15 04:55:56 +0000167 if (Subtarget->isTargetCygMing())
168 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000170 SwitchToSection(getObjFileLowering().SectionForGlobal(F, TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000172 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000174 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000175 case Function::LinkerPrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000176 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
178 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000180 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000183 case Function::LinkOnceAnyLinkage:
184 case Function::LinkOnceODRLinkage:
185 case Function::WeakAnyLinkage:
186 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000187 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000189 O << "\t.globl\t" << CurrentFnName << '\n';
190 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000192 O << "\t.globl\t" << CurrentFnName << "\n"
193 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000195 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 }
197 break;
198 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000199
200 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
202 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000203 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 else if (Subtarget->isTargetCygMing()) {
205 O << "\t.def\t " << CurrentFnName
206 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000207 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
209 << ";\t.endef\n";
210 }
211
212 O << CurrentFnName << ":\n";
213 // Add some workaround for linkonce linkage on Cygwin\MinGW
214 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000215 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000217}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000219/// runOnMachineFunction - This uses the printMachineInstruction()
220/// method to print assembly for each instruction.
221///
222bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
223 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000224 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000225 unsigned CC = F->getCallingConv();
226
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000227 SetupMachineFunction(MF);
228 O << "\n\n";
229
230 // Populate function information map. Actually, We don't want to populate
231 // non-stdcall or non-fastcall functions' information right now.
232 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
233 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
234
235 // Print out constants referenced by the function
236 EmitConstantPool(MF.getConstantPool());
237
238 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000239 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000240
241 // Print the 'header' of function
242 emitFunctionHeader(MF);
243
244 // Emit pre-function debug and/or EH information.
245 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000246 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000249 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
251 I != E; ++I) {
252 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000253 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
254 // This is an entry block or a block that's only reachable via a
255 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
256 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000257 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 O << '\n';
259 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000260 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
261 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000263 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000264 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 printMachineInstruction(II);
266 }
267 }
268
Dale Johannesenf35771f2008-04-08 00:37:56 +0000269 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
270 // If the function is empty, then we need to emit *something*. Otherwise,
271 // the function's label might be associated with something that it wasn't
272 // meant to be associated with. We emit a noop in this situation.
273 // We are assuming inline asms are code.
274 O << "\tnop\n";
275 }
276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000278 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000280 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000281 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000282 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
284 // Print out jump tables referenced by the function.
285 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000286
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000287 O.flush();
288
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 // We didn't modify anything.
290 return false;
291}
292
Chris Lattnerae8f9592009-07-13 21:53:19 +0000293/// printSymbolOperand - Print a raw symbol reference operand. This handles
294/// jump tables, constant pools, global address and external symbols, all of
295/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000296void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
297 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000298 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000299 case MachineOperand::MO_JumpTableIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000300 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000301 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000302 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000303 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000304 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000305 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000306 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000307 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000309 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000310
311 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000312 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
313 Suffix = "$stub";
314 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
315 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
316 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000317 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000318 Suffix = "$non_lazy_ptr";
319
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000320 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000321 if (Subtarget->isTargetCygMing())
322 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000323
Chris Lattner0cc2b792009-07-09 05:42:07 +0000324 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000325 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
326 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000327
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000328 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
329 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
330 GVStubs[Name] = Mang->getMangledName(GV);
331 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
332 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
333 HiddenGVStubs[Name] = Mang->getMangledName(GV);
334 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
335 FnStubs[Name] = Mang->getMangledName(GV);
336
337 // If the name begins with a dollar-sign, enclose it in parens. We do this
338 // to avoid having it look like an integer immediate to the assembler.
339 if (Name[0] == '$')
340 O << '(' << Name << ')';
341 else
342 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000343
Chris Lattner0cc2b792009-07-09 05:42:07 +0000344 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000345 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000347 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000348 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000349 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000350 FnStubs[Name+"$stub"] = Name;
351 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000352 }
353
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000354 // If the name begins with a dollar-sign, enclose it in parens. We do this
355 // to avoid having it look like an integer immediate to the assembler.
356 if (Name[0] == '$')
357 O << '(' << Name << ')';
358 else
359 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000360 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000362 }
Chris Lattner797a0782009-06-27 05:46:24 +0000363
364 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000366 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000367 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000368 break;
369 case X86II::MO_DARWIN_NONLAZY:
370 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
371 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000372 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000373 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000374 break;
375 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
376 O << " + [.-";
377 PrintPICBaseSymbol();
378 O << ']';
379 break;
380 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000381 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
382 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000383 O << '-';
384 PrintPICBaseSymbol();
385 break;
386 case X86II::MO_TLSGD: O << "@TLSGD"; break;
387 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
388 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
389 case X86II::MO_TPOFF: O << "@TPOFF"; break;
390 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
391 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
392 case X86II::MO_GOT: O << "@GOT"; break;
393 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000394 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 }
396}
397
Chris Lattnerae8f9592009-07-13 21:53:19 +0000398/// print_pcrel_imm - This is used to print an immediate value that ends up
399/// being encoded as a pc-relative value. These print slightly differently, for
400/// example, a $ is not emitted.
401void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
402 const MachineOperand &MO = MI->getOperand(OpNo);
403 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000404 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000405 case MachineOperand::MO_Immediate:
406 O << MO.getImm();
407 return;
408 case MachineOperand::MO_MachineBasicBlock:
409 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
410 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000411 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000412 case MachineOperand::MO_ExternalSymbol:
413 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000414 return;
415 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000416}
417
418
Chris Lattner207a0ca2009-07-13 21:41:08 +0000419
420void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
421 const char *Modifier) {
422 const MachineOperand &MO = MI->getOperand(OpNo);
423 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000424 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000425 case MachineOperand::MO_Register: {
426 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
427 "Virtual registers should not make it this far!");
428 O << '%';
429 unsigned Reg = MO.getReg();
430 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
431 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
432 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
433 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
434 Reg = getX86SubSuperRegister(Reg, VT);
435 }
436 O << TRI->getAsmName(Reg);
437 return;
438 }
439
440 case MachineOperand::MO_Immediate:
441 O << '$' << MO.getImm();
442 return;
443
444 case MachineOperand::MO_JumpTableIndex:
445 case MachineOperand::MO_ConstantPoolIndex:
446 case MachineOperand::MO_GlobalAddress:
447 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000448 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000449 printSymbolOperand(MO);
450 break;
451 }
452 }
453}
454
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000456 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 assert(value <= 7 && "Invalid ssecc argument!");
458 switch (value) {
459 case 0: O << "eq"; break;
460 case 1: O << "lt"; break;
461 case 2: O << "le"; break;
462 case 3: O << "unord"; break;
463 case 4: O << "neq"; break;
464 case 5: O << "nlt"; break;
465 case 6: O << "nle"; break;
466 case 7: O << "ord"; break;
467 }
468}
469
Rafael Espindolabca99f72009-04-08 21:14:34 +0000470void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000471 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000472 const MachineOperand &BaseReg = MI->getOperand(Op);
473 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 const MachineOperand &DispSpec = MI->getOperand(Op+3);
475
Chris Lattner791fd482009-07-09 00:27:29 +0000476 // If we really don't want to print out (rip), don't.
477 bool HasBaseReg = BaseReg.getReg() != 0;
478 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
479 BaseReg.getReg() == X86::RIP)
480 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000481
482 // HasParenPart - True if we will print out the () part of the mem ref.
483 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
484
485 if (DispSpec.isImm()) {
486 int DispVal = DispSpec.getImm();
487 if (DispVal || !HasParenPart)
488 O << DispVal;
489 } else {
490 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
491 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000492 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000493 }
494
495 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000496 assert(IndexReg.getReg() != X86::ESP &&
497 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000498
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000499 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000500 if (HasBaseReg)
501 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502
503 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000504 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000505 printOperand(MI, Op+2, Modifier);
506 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000508 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000510 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 }
512}
513
Rafael Espindolabca99f72009-04-08 21:14:34 +0000514void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000515 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000516 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000517 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000518 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000519 printOperand(MI, Op+4, Modifier);
520 O << ':';
521 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000522 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000523}
524
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000525void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000526 const MachineBasicBlock *MBB) const {
527 if (!TAI->getSetDirective())
528 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000529
530 // We don't need .set machinery if we have GOT-style relocations
531 if (Subtarget->isPICStyleGOT())
532 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000533
Evan Cheng6fb06762007-11-09 01:32:10 +0000534 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
535 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000536 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000537 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000538 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000539 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000540 else {
541 O << '-';
542 PrintPICBaseSymbol();
543 O << '\n';
544 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000545}
546
Chris Lattner14f791a2009-06-24 19:19:16 +0000547
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000549 PrintPICBaseSymbol();
550 O << '\n';
551 PrintPICBaseSymbol();
552 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553}
554
555
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000556void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
557 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000558 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000559 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
560 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
561
562 O << JTEntryDirective << ' ';
563
Chris Lattner2e9393c2009-07-10 21:00:45 +0000564 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000565 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
566 << '_' << uid << "_set_" << MBB->getNumber();
567 } else if (Subtarget->isPICStyleGOT()) {
568 printBasicBlockLabel(MBB, false, false, false);
569 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000570 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000571 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000572}
573
Chris Lattner8bb96e42009-06-15 04:42:32 +0000574bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 unsigned Reg = MO.getReg();
576 switch (Mode) {
577 default: return true; // Unknown mode.
578 case 'b': // Print QImode register
579 Reg = getX86SubSuperRegister(Reg, MVT::i8);
580 break;
581 case 'h': // Print QImode high register
582 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
583 break;
584 case 'w': // Print HImode register
585 Reg = getX86SubSuperRegister(Reg, MVT::i16);
586 break;
587 case 'k': // Print SImode register
588 Reg = getX86SubSuperRegister(Reg, MVT::i32);
589 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000590 case 'q': // Print DImode register
591 Reg = getX86SubSuperRegister(Reg, MVT::i64);
592 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 }
594
Evan Cheng00d04a72008-07-07 22:21:06 +0000595 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 return false;
597}
598
599/// PrintAsmOperand - Print out an operand for an inline asm expression.
600///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000601bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000602 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 const char *ExtraCode) {
604 // Does this asm operand have a single letter operand modifier?
605 if (ExtraCode && ExtraCode[0]) {
606 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000607
Chris Lattnerb6047e62009-07-13 21:48:33 +0000608 const MachineOperand &MO = MI->getOperand(OpNo);
609
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 switch (ExtraCode[0]) {
611 default: return true; // Unknown modifier.
612 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000613 if (MO.isImm())
614 O << MO.getImm();
615 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
616 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000617 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000618 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000620
621 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000622 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000623 O << '*';
624 printOperand(MI, OpNo);
625 return false;
626 }
627 return true;
628
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 case 'b': // Print QImode register
630 case 'h': // Print QImode high register
631 case 'w': // Print HImode register
632 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000633 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000634 if (MO.isReg())
635 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 printOperand(MI, OpNo);
637 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000638
Dale Johannesen375b88b2009-07-07 23:28:22 +0000639 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000640 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000642
Chris Lattnerb6047e62009-07-13 21:48:33 +0000643 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000644 // Note: this is a temporary solution. It should be handled target
645 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000646 if (MO.isImm()) {
647 O << -MO.getImm();
648 return false;
649 }
650 O << '-';
651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 printOperand(MI, OpNo);
655 return false;
656}
657
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000658bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000660 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000662 if (ExtraCode && ExtraCode[0]) {
663 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000664
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000665 switch (ExtraCode[0]) {
666 default: return true; // Unknown modifier.
667 case 'b': // Print QImode register
668 case 'h': // Print QImode high register
669 case 'w': // Print HImode register
670 case 'k': // Print SImode register
671 case 'q': // Print SImode register
672 // These only apply to registers, ignore on mem.
673 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000674 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000675 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000676 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000677 }
678 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 printMemReference(MI, OpNo);
680 return false;
681}
682
Chris Lattnerf5da5902009-06-20 07:03:18 +0000683static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
684 // Convert registers in the addr mode according to subreg64.
685 for (unsigned i = 0; i != 4; ++i) {
686 if (!MI->getOperand(i).isReg()) continue;
687
688 unsigned Reg = MI->getOperand(i).getReg();
689 if (Reg == 0) continue;
690
691 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
692 }
693}
694
Bill Wendlingb3b11262009-02-06 21:45:08 +0000695/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
696/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697///
698void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
699 ++EmittedInsts;
700
Chris Lattner19b1bd52009-06-19 00:47:33 +0000701 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000702 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
703 O << "\t";
704 printInlineAsm(MI);
705 return;
706 } else if (MI->isLabel()) {
707 printLabel(MI);
708 return;
709 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
710 printDeclare(MI);
711 return;
712 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
713 printImplicitDef(MI);
714 return;
715 }
716
Chris Lattnere67184e2009-06-19 23:59:57 +0000717 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000718 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000719
720 TmpInst.setOpcode(MI->getOpcode());
721
722 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
723 const MachineOperand &MO = MI->getOperand(i);
724
725 MCOperand MCOp;
726 if (MO.isReg()) {
727 MCOp.MakeReg(MO.getReg());
728 } else if (MO.isImm()) {
729 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000730 } else if (MO.isMBB()) {
731 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000732 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000733 llvm_unreachable("Unimp");
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000734 }
735
736 TmpInst.addOperand(MCOp);
737 }
738
Chris Lattner6b9f7742009-06-20 07:59:10 +0000739 switch (TmpInst.getOpcode()) {
740 case X86::LEA64_32r:
741 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000742 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000743 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000744 }
745
Chris Lattner19b1bd52009-06-19 00:47:33 +0000746 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000747 printInstruction(&TmpInst);
748 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000749 }
750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 // Call the autogenerated instruction printer routines.
752 printInstruction(MI);
753}
754
Chris Lattnerae982212009-07-21 18:38:57 +0000755void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000756 const TargetData *TD = TM.getTargetData();
757
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000758 if (!GVar->hasInitializer())
759 return; // External global require no code
760
761 // Check to see if this is a special global used by LLVM, if so, emit it.
762 if (EmitSpecialLLVMGlobal(GVar)) {
763 if (Subtarget->isTargetDarwin() &&
764 TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000765 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000766 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000767 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000768 O << ".reference .destructors_used\n";
769 }
770 return;
771 }
772
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000773 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000774 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000775 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000776 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000777 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000778 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000779 unsigned Align = TD->getPreferredAlignmentLog(GVar);
780
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000781 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000782
783 if (Subtarget->isTargetELF())
784 O << "\t.type\t" << name << ",@object\n";
785
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000786 const Section *TheSection = getObjFileLowering().SectionForGlobal(GVar, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000787 SwitchToSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000788
Evan Chengcf84b142009-02-18 02:19:52 +0000789 if (C->isNullValue() && !GVar->hasSection() &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000790 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000791 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000792 if (GVar->hasExternalLinkage()) {
793 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000794 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000796 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000797 return;
798 }
799 }
800
801 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000802 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000803 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000804
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000806 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000811 O << "\t.globl " << name << '\n'
812 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000814 O << name << ":";
815 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000816 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000817 PrintUnmangledNameSafely(GVar, O);
818 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000819 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 EmitGlobalConstant(C);
821 return;
822 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000823 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000824 if (TAI->getCOMMDirectiveTakesAlignment())
825 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000826 }
827 } else {
828 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000829 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000830 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000831 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000832 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000833 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000834 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000835 }
Evan Cheng11db8142009-03-24 00:17:40 +0000836 if (VerboseAsm) {
837 O << "\t\t" << TAI->getCommentString() << ' ';
838 PrintUnmangledNameSafely(GVar, O);
839 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000840 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000841 return;
842 }
843 }
844
845 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000846 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000847 case GlobalValue::LinkOnceAnyLinkage:
848 case GlobalValue::LinkOnceODRLinkage:
849 case GlobalValue::WeakAnyLinkage:
850 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000851 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000852 O << "\t.globl " << name << '\n'
853 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000855 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000856 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000858 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000859 }
860 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000861 case GlobalValue::DLLExportLinkage:
862 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 // FIXME: appending linkage variables should go into a section of
864 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000865 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000867 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000868 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000869 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000870 case GlobalValue::LinkerPrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000871 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000873 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000874 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000875 }
876
877 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000878 O << name << ":";
879 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000880 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000881 PrintUnmangledNameSafely(GVar, O);
882 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000883 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000884 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000885 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000886
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000887 EmitGlobalConstant(C);
888}
889
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000890bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000891 // Print out module-level global variables here.
892 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000893 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000894 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000895 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000896 }
897
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000898 if (Subtarget->isTargetDarwin()) {
899 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000900
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000901 // Add the (possibly multiple) personalities to the set of global value
902 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000903 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000904 const std::vector<Function*> &Personalities = MMI->getPersonalities();
905 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000906 if (Personalities[i])
907 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
908 true /*private label*/)] =
909 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000910 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000911 }
912
Chris Lattner2e860262009-06-24 18:24:09 +0000913 // Output stubs for dynamically-linked functions
914 if (!FnStubs.empty()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000915 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
916 "self_modifying_code+pure_instructions,5", 0);
917 for (StringMap<std::string>::iterator I = FnStubs.begin(),
918 E = FnStubs.end(); I != E; ++I)
919 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
920 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000921 O << '\n';
922 }
923
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000924 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000925 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000926 SwitchToDataSection(
927 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000928 for (StringMap<std::string>::iterator I = GVStubs.begin(),
929 E = GVStubs.end(); I != E; ++I)
930 O << I->getKeyData() << ":\n\t.indirect_symbol "
931 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000932 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000933
Evan Chenga65854f2008-12-05 01:06:39 +0000934 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000935 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000936 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000937 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
938 E = HiddenGVStubs.end(); I != E; ++I)
939 O << I->getKeyData() << ":\n" << TAI->getData32bitsDirective()
940 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000941 }
942
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000943 // Funny Darwin hack: This flag tells the linker that no global symbols
944 // contain code that falls through to other global symbols (e.g. the obvious
945 // implementation of multiple entry points). If this doesn't occur, the
946 // linker can safely perform dead code stripping. Since LLVM never
947 // generates code that does this, it is always safe to set.
948 O << "\t.subsections_via_symbols\n";
949 } else if (Subtarget->isTargetCygMing()) {
950 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +0000951 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000952 i != e; ++i) {
953 O << "\t.def\t " << i->getKeyData()
954 << ";\t.scl\t" << COFF::C_EXT
955 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
956 << ";\t.endef\n";
957 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000958 }
Chris Lattner5ec2b662009-06-24 05:47:59 +0000959
Chris Lattnerdb191f02009-06-24 18:52:01 +0000960
961 // Output linker support code for dllexported globals on windows.
962 if (!DLLExportedGVs.empty()) {
963 SwitchToDataSection(".section .drectve");
964
965 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
966 e = DLLExportedGVs.end(); i != e; ++i)
967 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
968 }
969
970 if (!DLLExportedFns.empty()) {
971 SwitchToDataSection(".section .drectve");
972
973 for (StringSet<>::iterator i = DLLExportedFns.begin(),
974 e = DLLExportedFns.end();
975 i != e; ++i)
976 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
977 }
978
Chris Lattnerdb191f02009-06-24 18:52:01 +0000979 // Do common shutdown.
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000980 return AsmPrinter::doFinalization(M);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000981}
982
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983// Include the auto-generated portion of the assembly writer.
984#include "X86GenAsmWriter.inc"