blob: 74145dcfce0246b24a664e73968f599690a0dbea [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 Patelf667ab42009-06-25 00:47:42 +000026#include "llvm/MDNode.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"
40#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041using namespace llvm;
42
43STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
Chris Lattner19b1bd52009-06-19 00:47:33 +000045static cl::opt<bool> NewAsmPrinter("experimental-asm-printer",
46 cl::Hidden);
47
Chris Lattner2e845952009-06-24 19:44:36 +000048//===----------------------------------------------------------------------===//
49// Primitive Helper Functions.
50//===----------------------------------------------------------------------===//
Chris Lattner14f791a2009-06-24 19:19:16 +000051
52void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 if (Subtarget->isTargetDarwin())
Chris Lattner14f791a2009-06-24 19:19:16 +000054 O << "\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 else if (Subtarget->isTargetELF())
Chris Lattner021ad452009-07-08 23:09:14 +000056 O << ".Lllvm$" << getFunctionNumber() << ".$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 else
Edwin Törökbd448e32009-07-14 16:55:14 +000058 llvm_unreachable("Don't know how to print PIC label!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059}
60
Chris Lattner2e845952009-06-24 19:44:36 +000061/// PrintUnmangledNameSafely - Print out the printable characters in the name.
62/// Don't print things like \\n or \\0.
David Greene302008d2009-07-14 20:18:05 +000063static void PrintUnmangledNameSafely(const Value *V,
64 formatted_raw_ostream &OS) {
Chris Lattner2e845952009-06-24 19:44:36 +000065 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
66 Name != E; ++Name)
67 if (isprint(*Name))
68 OS << *Name;
69}
Chris Lattner14f791a2009-06-24 19:19:16 +000070
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000071static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
72 const TargetData *TD) {
73 X86MachineFunctionInfo Info;
74 uint64_t Size = 0;
75
76 switch (F->getCallingConv()) {
77 case CallingConv::X86_StdCall:
78 Info.setDecorationStyle(StdCall);
79 break;
80 case CallingConv::X86_FastCall:
81 Info.setDecorationStyle(FastCall);
82 break;
83 default:
84 return Info;
85 }
86
87 unsigned argNum = 1;
88 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
89 AI != AE; ++AI, ++argNum) {
90 const Type* Ty = AI->getType();
91
92 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000093 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000094 Ty = cast<PointerType>(Ty)->getElementType();
95
96 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000097 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000098 }
99
100 // We're not supporting tooooo huge arguments :)
101 Info.setBytesToPopOnReturn((unsigned int)Size);
102 return Info;
103}
104
Chris Lattner8cfe9142009-07-15 04:55:56 +0000105/// DecorateCygMingName - Query FunctionInfoMap and use this information for
106/// various name decorations for Cygwin and MingW.
107void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
108 const GlobalValue *GV) {
109 assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
110
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000111 const Function *F = dyn_cast<Function>(GV);
112 if (!F) return;
113
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000114 // Save function name for later type emission.
Chris Lattner8cfe9142009-07-15 04:55:56 +0000115 if (F->isDeclaration())
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000116 CygMingStubs.insert(Name);
117
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000118 // We don't want to decorate non-stdcall or non-fastcall functions right now
119 unsigned CC = F->getCallingConv();
120 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
121 return;
122
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000123
124 const X86MachineFunctionInfo *Info;
Chris Lattner8cfe9142009-07-15 04:55:56 +0000125
126 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000127 if (info_item == FunctionInfoMap.end()) {
128 // Calculate apropriate function info and populate map
129 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
130 Info = &FunctionInfoMap[F];
131 } else {
132 Info = &info_item->second;
133 }
134
135 const FunctionType *FT = F->getFunctionType();
136 switch (Info->getDecorationStyle()) {
137 case None:
138 break;
139 case StdCall:
140 // "Pure" variadic functions do not receive @0 suffix.
141 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
142 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
143 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
144 break;
145 case FastCall:
146 // "Pure" variadic functions do not receive @0 suffix.
147 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
148 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
149 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
150
151 if (Name[0] == '_') {
152 Name[0] = '@';
153 } else {
154 Name = '@' + Name;
155 }
156 break;
157 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000158 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000159 }
160}
161
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000162void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Bill Wendling25a8ae32009-06-30 22:38:32 +0000163 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
Chris Lattner8cfe9142009-07-15 04:55:56 +0000166 if (Subtarget->isTargetCygMing())
167 DecorateCygMingName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000169 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000171 default: llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000173 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000174 case Function::LinkerPrivateLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 break;
177 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000179 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000182 case Function::LinkOnceAnyLinkage:
183 case Function::LinkOnceODRLinkage:
184 case Function::WeakAnyLinkage:
185 case Function::WeakODRLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000186 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << '\n';
189 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000191 O << "\t.globl\t" << CurrentFnName << "\n"
192 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000194 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196 break;
197 }
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000198
199 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
201 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000202 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 else if (Subtarget->isTargetCygMing()) {
204 O << "\t.def\t " << CurrentFnName
205 << ";\t.scl\t" <<
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000206 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
208 << ";\t.endef\n";
209 }
210
211 O << CurrentFnName << ":\n";
212 // Add some workaround for linkonce linkage on Cygwin\MinGW
213 if (Subtarget->isTargetCygMing() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000214 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000216}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000218/// runOnMachineFunction - This uses the printMachineInstruction()
219/// method to print assembly for each instruction.
220///
221bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
222 const Function *F = MF.getFunction();
Bill Wendlingb3b11262009-02-06 21:45:08 +0000223 this->MF = &MF;
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000224 unsigned CC = F->getCallingConv();
225
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000226 SetupMachineFunction(MF);
227 O << "\n\n";
228
229 // Populate function information map. Actually, We don't want to populate
230 // non-stdcall or non-fastcall functions' information right now.
231 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
232 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
233
234 // Print out constants referenced by the function
235 EmitConstantPool(MF.getConstantPool());
236
237 if (F->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000238 DLLExportedFns.insert(Mang->getMangledName(F));
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000239
240 // Print the 'header' of function
241 emitFunctionHeader(MF);
242
243 // Emit pre-function debug and/or EH information.
244 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000245 DW->BeginFunction(&MF);
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000248 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
250 I != E; ++I) {
251 // Print a label for the basic block.
Dan Gohmand38f8762009-03-31 18:39:13 +0000252 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
253 // This is an entry block or a block that's only reachable via a
254 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
255 } else {
Evan Cheng11db8142009-03-24 00:17:40 +0000256 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 O << '\n';
258 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000259 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
260 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000262 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000263 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 printMachineInstruction(II);
265 }
266 }
267
Dale Johannesenf35771f2008-04-08 00:37:56 +0000268 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
269 // If the function is empty, then we need to emit *something*. Otherwise,
270 // the function's label might be associated with something that it wasn't
271 // meant to be associated with. We emit a noop in this situation.
272 // We are assuming inline asms are code.
273 O << "\tnop\n";
274 }
275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000277 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000279 // Emit post-function debug information.
Devang Patel4d438ce2009-06-19 23:21:20 +0000280 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
Devang Patelaa1e8432009-01-08 23:40:34 +0000281 DW->EndFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
283 // Print out jump tables referenced by the function.
284 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000285
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000286 O.flush();
287
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 // We didn't modify anything.
289 return false;
290}
291
Chris Lattnerae8f9592009-07-13 21:53:19 +0000292/// printSymbolOperand - Print a raw symbol reference operand. This handles
293/// jump tables, constant pools, global address and external symbols, all of
294/// which print to a label with various suffixes for relocation types etc.
Chris Lattner207a0ca2009-07-13 21:41:08 +0000295void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
296 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000297 default: llvm_unreachable("unknown symbol type!");
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000298 case MachineOperand::MO_JumpTableIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000299 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000300 << MO.getIndex();
Chris Lattner797a0782009-06-27 05:46:24 +0000301 break;
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000302 case MachineOperand::MO_ConstantPoolIndex:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000303 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000304 << MO.getIndex();
Chris Lattner40f56902009-06-26 20:00:05 +0000305 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000306 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000308 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000309
310 const char *Suffix = "";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000311 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
312 Suffix = "$stub";
313 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
314 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
315 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000316 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000317 Suffix = "$non_lazy_ptr";
318
Chris Lattnerbd5f2922009-07-15 01:53:36 +0000319 std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
Chris Lattner8cfe9142009-07-15 04:55:56 +0000320 if (Subtarget->isTargetCygMing())
321 DecorateCygMingName(Name, GV);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000322
Chris Lattner0cc2b792009-07-09 05:42:07 +0000323 // Handle dllimport linkage.
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000324 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
325 Name = "__imp_" + Name;
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000326
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000327 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
328 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE)
329 GVStubs[Name] = Mang->getMangledName(GV);
330 else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY ||
331 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
332 HiddenGVStubs[Name] = Mang->getMangledName(GV);
333 else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
334 FnStubs[Name] = Mang->getMangledName(GV);
335
336 // If the name begins with a dollar-sign, enclose it in parens. We do this
337 // to avoid having it look like an integer immediate to the assembler.
338 if (Name[0] == '$')
339 O << '(' << Name << ')';
340 else
341 O << Name;
Chris Lattnerdabfe572009-06-21 02:22:53 +0000342
Chris Lattner0cc2b792009-07-09 05:42:07 +0000343 printOffset(MO.getOffset());
Chris Lattner797a0782009-06-27 05:46:24 +0000344 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000346 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner08be7a72009-07-15 03:01:23 +0000347 std::string Name = Mang->makeNameProper(MO.getSymbolName());
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000348 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000349 FnStubs[Name+"$stub"] = Name;
350 Name += "$stub";
Daniel Dunbarc5c467c2009-07-14 15:57:55 +0000351 }
352
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000353 // If the name begins with a dollar-sign, enclose it in parens. We do this
354 // to avoid having it look like an integer immediate to the assembler.
355 if (Name[0] == '$')
356 O << '(' << Name << ')';
357 else
358 O << Name;
Chris Lattner797a0782009-06-27 05:46:24 +0000359 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 }
Chris Lattnera45d3aa2009-07-13 22:28:21 +0000361 }
Chris Lattner797a0782009-06-27 05:46:24 +0000362
363 switch (MO.getTargetFlags()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000365 llvm_unreachable("Unknown target flag on GV operand");
Chris Lattner9ab4e662009-07-09 00:58:53 +0000366 case X86II::MO_NO_FLAG: // No flag.
Chris Lattnera3bde622009-07-09 06:59:17 +0000367 break;
368 case X86II::MO_DARWIN_NONLAZY:
369 case X86II::MO_DARWIN_HIDDEN_NONLAZY:
370 case X86II::MO_DLLIMPORT:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000371 case X86II::MO_DARWIN_STUB:
Chris Lattnera3bde622009-07-09 06:59:17 +0000372 // These affect the name of the symbol, not any suffix.
Chris Lattner797a0782009-06-27 05:46:24 +0000373 break;
374 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
375 O << " + [.-";
376 PrintPICBaseSymbol();
377 O << ']';
378 break;
379 case X86II::MO_PIC_BASE_OFFSET:
Chris Lattnera3bde622009-07-09 06:59:17 +0000380 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
381 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Chris Lattner797a0782009-06-27 05:46:24 +0000382 O << '-';
383 PrintPICBaseSymbol();
384 break;
385 case X86II::MO_TLSGD: O << "@TLSGD"; break;
386 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
387 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
388 case X86II::MO_TPOFF: O << "@TPOFF"; break;
389 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
390 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
391 case X86II::MO_GOT: O << "@GOT"; break;
392 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000393 case X86II::MO_PLT: O << "@PLT"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 }
395}
396
Chris Lattnerae8f9592009-07-13 21:53:19 +0000397/// print_pcrel_imm - This is used to print an immediate value that ends up
398/// being encoded as a pc-relative value. These print slightly differently, for
399/// example, a $ is not emitted.
400void X86ATTAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
401 const MachineOperand &MO = MI->getOperand(OpNo);
402 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000403 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattnerae8f9592009-07-13 21:53:19 +0000404 case MachineOperand::MO_Immediate:
405 O << MO.getImm();
406 return;
407 case MachineOperand::MO_MachineBasicBlock:
408 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
409 return;
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000410 case MachineOperand::MO_GlobalAddress:
Chris Lattnere1cf8a12009-07-13 22:07:30 +0000411 case MachineOperand::MO_ExternalSymbol:
412 printSymbolOperand(MO);
Chris Lattnerae8f9592009-07-13 21:53:19 +0000413 return;
414 }
Chris Lattnerae8f9592009-07-13 21:53:19 +0000415}
416
417
Chris Lattner207a0ca2009-07-13 21:41:08 +0000418
419void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
420 const char *Modifier) {
421 const MachineOperand &MO = MI->getOperand(OpNo);
422 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000423 default: llvm_unreachable("unknown operand type!");
Chris Lattner207a0ca2009-07-13 21:41:08 +0000424 case MachineOperand::MO_Register: {
425 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
426 "Virtual registers should not make it this far!");
427 O << '%';
428 unsigned Reg = MO.getReg();
429 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
430 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
431 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
432 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
433 Reg = getX86SubSuperRegister(Reg, VT);
434 }
435 O << TRI->getAsmName(Reg);
436 return;
437 }
438
439 case MachineOperand::MO_Immediate:
440 O << '$' << MO.getImm();
441 return;
442
443 case MachineOperand::MO_JumpTableIndex:
444 case MachineOperand::MO_ConstantPoolIndex:
445 case MachineOperand::MO_GlobalAddress:
446 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerb6047e62009-07-13 21:48:33 +0000447 O << '$';
Chris Lattner207a0ca2009-07-13 21:41:08 +0000448 printSymbolOperand(MO);
449 break;
450 }
451 }
452}
453
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000455 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 assert(value <= 7 && "Invalid ssecc argument!");
457 switch (value) {
458 case 0: O << "eq"; break;
459 case 1: O << "lt"; break;
460 case 2: O << "le"; break;
461 case 3: O << "unord"; break;
462 case 4: O << "neq"; break;
463 case 5: O << "nlt"; break;
464 case 6: O << "nle"; break;
465 case 7: O << "ord"; break;
466 }
467}
468
Rafael Espindolabca99f72009-04-08 21:14:34 +0000469void X86ATTAsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000470 const char *Modifier) {
Chris Lattner791fd482009-07-09 00:27:29 +0000471 const MachineOperand &BaseReg = MI->getOperand(Op);
472 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 const MachineOperand &DispSpec = MI->getOperand(Op+3);
474
Chris Lattner791fd482009-07-09 00:27:29 +0000475 // If we really don't want to print out (rip), don't.
476 bool HasBaseReg = BaseReg.getReg() != 0;
477 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
478 BaseReg.getReg() == X86::RIP)
479 HasBaseReg = false;
Chris Lattnerec112ef2009-07-09 00:32:12 +0000480
481 // HasParenPart - True if we will print out the () part of the mem ref.
482 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
483
484 if (DispSpec.isImm()) {
485 int DispVal = DispSpec.getImm();
486 if (DispVal || !HasParenPart)
487 O << DispVal;
488 } else {
489 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
490 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattnerb6047e62009-07-13 21:48:33 +0000491 printSymbolOperand(MI->getOperand(Op+3));
Chris Lattnerec112ef2009-07-09 00:32:12 +0000492 }
493
494 if (HasParenPart) {
Chris Lattner791fd482009-07-09 00:27:29 +0000495 assert(IndexReg.getReg() != X86::ESP &&
496 "X86 doesn't allow scaling by ESP");
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000497
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000498 O << '(';
Chris Lattner791fd482009-07-09 00:27:29 +0000499 if (HasBaseReg)
500 printOperand(MI, Op, Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501
502 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000503 O << ',';
Chris Lattner791fd482009-07-09 00:27:29 +0000504 printOperand(MI, Op+2, Modifier);
505 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000507 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000509 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 }
511}
512
Rafael Espindolabca99f72009-04-08 21:14:34 +0000513void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000514 const char *Modifier) {
Rafael Espindolabca99f72009-04-08 21:14:34 +0000515 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner791fd482009-07-09 00:27:29 +0000516 const MachineOperand &Segment = MI->getOperand(Op+4);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000517 if (Segment.getReg()) {
Chris Lattner791fd482009-07-09 00:27:29 +0000518 printOperand(MI, Op+4, Modifier);
519 O << ':';
520 }
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000521 printLeaMemReference(MI, Op, Modifier);
Rafael Espindolabca99f72009-04-08 21:14:34 +0000522}
523
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000524void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000525 const MachineBasicBlock *MBB) const {
526 if (!TAI->getSetDirective())
527 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000528
529 // We don't need .set machinery if we have GOT-style relocations
530 if (Subtarget->isPICStyleGOT())
531 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000532
Evan Cheng6fb06762007-11-09 01:32:10 +0000533 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
534 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000535 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000536 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000537 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000538 << '_' << uid << '\n';
Chris Lattner14f791a2009-06-24 19:19:16 +0000539 else {
540 O << '-';
541 PrintPICBaseSymbol();
542 O << '\n';
543 }
Evan Cheng6fb06762007-11-09 01:32:10 +0000544}
545
Chris Lattner14f791a2009-06-24 19:19:16 +0000546
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Chris Lattner14f791a2009-06-24 19:19:16 +0000548 PrintPICBaseSymbol();
549 O << '\n';
550 PrintPICBaseSymbol();
551 O << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552}
553
554
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000555void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
556 const MachineBasicBlock *MBB,
Chris Lattnerb1f8d4f2009-07-10 21:57:21 +0000557 unsigned uid) const {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000558 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
559 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
560
561 O << JTEntryDirective << ' ';
562
Chris Lattner2e9393c2009-07-10 21:00:45 +0000563 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
Chris Lattner4a948932009-07-10 20:47:30 +0000564 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
565 << '_' << uid << "_set_" << MBB->getNumber();
566 } else if (Subtarget->isPICStyleGOT()) {
567 printBasicBlockLabel(MBB, false, false, false);
568 O << "@GOTOFF";
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000569 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000570 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000571}
572
Chris Lattner8bb96e42009-06-15 04:42:32 +0000573bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 unsigned Reg = MO.getReg();
575 switch (Mode) {
576 default: return true; // Unknown mode.
577 case 'b': // Print QImode register
578 Reg = getX86SubSuperRegister(Reg, MVT::i8);
579 break;
580 case 'h': // Print QImode high register
581 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
582 break;
583 case 'w': // Print HImode register
584 Reg = getX86SubSuperRegister(Reg, MVT::i16);
585 break;
586 case 'k': // Print SImode register
587 Reg = getX86SubSuperRegister(Reg, MVT::i32);
588 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000589 case 'q': // Print DImode register
590 Reg = getX86SubSuperRegister(Reg, MVT::i64);
591 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 }
593
Evan Cheng00d04a72008-07-07 22:21:06 +0000594 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 return false;
596}
597
598/// PrintAsmOperand - Print out an operand for an inline asm expression.
599///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000600bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000601 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 const char *ExtraCode) {
603 // Does this asm operand have a single letter operand modifier?
604 if (ExtraCode && ExtraCode[0]) {
605 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000606
Chris Lattnerb6047e62009-07-13 21:48:33 +0000607 const MachineOperand &MO = MI->getOperand(OpNo);
608
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 switch (ExtraCode[0]) {
610 default: return true; // Unknown modifier.
611 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnerb6047e62009-07-13 21:48:33 +0000612 if (MO.isImm())
613 O << MO.getImm();
614 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
615 printSymbolOperand(MO);
Chris Lattner207a0ca2009-07-13 21:41:08 +0000616 else
Chris Lattnerb6047e62009-07-13 21:48:33 +0000617 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 return false;
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000619
620 case 'A': // Print '*' before a register (it must be a register)
Chris Lattnerb6047e62009-07-13 21:48:33 +0000621 if (MO.isReg()) {
Dale Johannesen7ad82e52009-07-09 20:06:27 +0000622 O << '*';
623 printOperand(MI, OpNo);
624 return false;
625 }
626 return true;
627
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 case 'b': // Print QImode register
629 case 'h': // Print QImode high register
630 case 'w': // Print HImode register
631 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000632 case 'q': // Print DImode register
Chris Lattnerb6047e62009-07-13 21:48:33 +0000633 if (MO.isReg())
634 return printAsmMRegister(MO, ExtraCode[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 printOperand(MI, OpNo);
636 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000637
Dale Johannesen375b88b2009-07-07 23:28:22 +0000638 case 'P': // This is the operand of a call, treat specially.
Chris Lattner85dbcd52009-07-09 00:39:19 +0000639 print_pcrel_imm(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 return false;
Evan Cheng9eba17b2009-06-26 22:00:19 +0000641
Chris Lattnerb6047e62009-07-13 21:48:33 +0000642 case 'n': // Negate the immediate or print a '-' before the operand.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000643 // Note: this is a temporary solution. It should be handled target
644 // independently as part of the 'MC' work.
Evan Cheng9eba17b2009-06-26 22:00:19 +0000645 if (MO.isImm()) {
646 O << -MO.getImm();
647 return false;
648 }
649 O << '-';
650 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000652
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 printOperand(MI, OpNo);
654 return false;
655}
656
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000657bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000659 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000661 if (ExtraCode && ExtraCode[0]) {
662 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000663
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000664 switch (ExtraCode[0]) {
665 default: return true; // Unknown modifier.
666 case 'b': // Print QImode register
667 case 'h': // Print QImode high register
668 case 'w': // Print HImode register
669 case 'k': // Print SImode register
670 case 'q': // Print SImode register
671 // These only apply to registers, ignore on mem.
672 break;
Chris Lattnerd1595722009-01-23 22:33:40 +0000673 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000674 printMemReference(MI, OpNo, "no-rip");
Chris Lattnerd1595722009-01-23 22:33:40 +0000675 return false;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000676 }
677 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 printMemReference(MI, OpNo);
679 return false;
680}
681
Chris Lattnerf5da5902009-06-20 07:03:18 +0000682static void lower_lea64_32mem(MCInst *MI, unsigned OpNo) {
683 // Convert registers in the addr mode according to subreg64.
684 for (unsigned i = 0; i != 4; ++i) {
685 if (!MI->getOperand(i).isReg()) continue;
686
687 unsigned Reg = MI->getOperand(i).getReg();
688 if (Reg == 0) continue;
689
690 MI->getOperand(i).setReg(getX86SubSuperRegister(Reg, MVT::i64));
691 }
692}
693
Bill Wendlingb3b11262009-02-06 21:45:08 +0000694/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
695/// AT&T syntax to the current output stream.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696///
697void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
698 ++EmittedInsts;
699
Chris Lattner19b1bd52009-06-19 00:47:33 +0000700 if (NewAsmPrinter) {
Chris Lattnerf5da5902009-06-20 07:03:18 +0000701 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
702 O << "\t";
703 printInlineAsm(MI);
704 return;
705 } else if (MI->isLabel()) {
706 printLabel(MI);
707 return;
708 } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {
709 printDeclare(MI);
710 return;
711 } else if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
712 printImplicitDef(MI);
713 return;
714 }
715
Chris Lattnere67184e2009-06-19 23:59:57 +0000716 O << "NEW: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000717 MCInst TmpInst;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000718
719 TmpInst.setOpcode(MI->getOpcode());
720
721 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
722 const MachineOperand &MO = MI->getOperand(i);
723
724 MCOperand MCOp;
725 if (MO.isReg()) {
726 MCOp.MakeReg(MO.getReg());
727 } else if (MO.isImm()) {
728 MCOp.MakeImm(MO.getImm());
Chris Lattnerf5da5902009-06-20 07:03:18 +0000729 } else if (MO.isMBB()) {
730 MCOp.MakeMBBLabel(getFunctionNumber(), MO.getMBB()->getNumber());
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000731 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000732 llvm_unreachable("Unimp");
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000733 }
734
735 TmpInst.addOperand(MCOp);
736 }
737
Chris Lattner6b9f7742009-06-20 07:59:10 +0000738 switch (TmpInst.getOpcode()) {
739 case X86::LEA64_32r:
740 // Handle the 'subreg rewriting' for the lea64_32mem operand.
Chris Lattnerf5da5902009-06-20 07:03:18 +0000741 lower_lea64_32mem(&TmpInst, 1);
Chris Lattner6b9f7742009-06-20 07:59:10 +0000742 break;
Chris Lattner4ebc52f2009-06-20 00:49:26 +0000743 }
744
Chris Lattner19b1bd52009-06-19 00:47:33 +0000745 // FIXME: Convert TmpInst.
Chris Lattnere67184e2009-06-19 23:59:57 +0000746 printInstruction(&TmpInst);
747 O << "OLD: ";
Chris Lattner19b1bd52009-06-19 00:47:33 +0000748 }
749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 // Call the autogenerated instruction printer routines.
751 printInstruction(MI);
752}
753
Devang Patelc20079e2009-06-20 01:00:07 +0000754/// doInitialization
755bool X86ATTAsmPrinter::doInitialization(Module &M) {
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000756 if (NewAsmPrinter) {
757 Context = new MCContext();
758 // FIXME: Send this to "O" instead of outs(). For now, we force it to
759 // stdout to make it easy to compare.
760 Streamer = createAsmStreamer(*Context, outs());
761 }
762
Devang Patelc20079e2009-06-20 01:00:07 +0000763 return AsmPrinter::doInitialization(M);
764}
765
Chris Lattnerae982212009-07-21 18:38:57 +0000766void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000767 const TargetData *TD = TM.getTargetData();
768
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000769 if (!GVar->hasInitializer())
770 return; // External global require no code
771
772 // Check to see if this is a special global used by LLVM, if so, emit it.
773 if (EmitSpecialLLVMGlobal(GVar)) {
774 if (Subtarget->isTargetDarwin() &&
775 TM.getRelocationModel() == Reloc::Static) {
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000776 if (GVar->isName("llvm.global_ctors"))
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000777 O << ".reference .constructors_used\n";
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000778 else if (GVar->isName("llvm.global_dtors"))
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000779 O << ".reference .destructors_used\n";
780 }
781 return;
782 }
783
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000784 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000785 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000786 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000787 return;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000788 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000789 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000790 unsigned Align = TD->getPreferredAlignmentLog(GVar);
791
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000792 printVisibility(name, GVar->getVisibility());
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000793
794 if (Subtarget->isTargetELF())
795 O << "\t.type\t" << name << ",@object\n";
796
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000797 const Section *TheSection = TAI->SectionForGlobal(GVar);
798 SwitchToSection(TheSection);
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000799
Evan Chengcf84b142009-02-18 02:19:52 +0000800 if (C->isNullValue() && !GVar->hasSection() &&
801 !(Subtarget->isTargetDarwin() &&
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000802 TheSection->getFlags() == SectionKind::RODataMergeStr)) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000803 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000804 if (GVar->hasExternalLinkage()) {
805 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000806 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000807 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000808 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000809 return;
810 }
811 }
812
813 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000814 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000816
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000817 if (TAI->getLCOMMDirective() != NULL) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000818 if (GVar->hasLocalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000819 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000820 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000821 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000822 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000823 O << "\t.globl " << name << '\n'
824 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000826 O << name << ":";
827 if (VerboseAsm) {
Evan Cheng4c7969e2009-03-25 01:08:42 +0000828 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000829 PrintUnmangledNameSafely(GVar, O);
830 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000831 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 EmitGlobalConstant(C);
833 return;
834 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16876eb2008-08-08 18:25:52 +0000836 if (TAI->getCOMMDirectiveTakesAlignment())
837 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 }
839 } else {
840 if (!Subtarget->isTargetCygMing()) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000841 if (GVar->hasLocalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000842 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000843 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000844 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000845 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000846 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000847 }
Evan Cheng11db8142009-03-24 00:17:40 +0000848 if (VerboseAsm) {
849 O << "\t\t" << TAI->getCommentString() << ' ';
850 PrintUnmangledNameSafely(GVar, O);
851 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000852 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000853 return;
854 }
855 }
856
857 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000858 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000859 case GlobalValue::LinkOnceAnyLinkage:
860 case GlobalValue::LinkOnceODRLinkage:
861 case GlobalValue::WeakAnyLinkage:
862 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000864 O << "\t.globl " << name << '\n'
865 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000867 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000868 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000869 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000870 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000871 }
872 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000873 case GlobalValue::DLLExportLinkage:
874 case GlobalValue::AppendingLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000875 // FIXME: appending linkage variables should go into a section of
876 // their name or something. For now, just emit them as external.
Evan Cheng630c5612008-08-08 06:43:59 +0000877 case GlobalValue::ExternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000878 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000879 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000880 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000881 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000882 case GlobalValue::LinkerPrivateLinkage:
Evan Cheng630c5612008-08-08 06:43:59 +0000883 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000884 break;
Evan Cheng630c5612008-08-08 06:43:59 +0000885 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000886 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000887 }
888
889 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000890 O << name << ":";
891 if (VerboseAsm){
Evan Cheng4c7969e2009-03-25 01:08:42 +0000892 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
Evan Cheng11db8142009-03-24 00:17:40 +0000893 PrintUnmangledNameSafely(GVar, O);
894 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000895 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000896 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000897 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000898
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000899 EmitGlobalConstant(C);
900}
901
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000902bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000903 // Print out module-level global variables here.
904 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000905 I != E; ++I) {
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000906 if (I->hasDLLExportLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000907 DLLExportedGVs.insert(Mang->getMangledName(I));
Anton Korobeynikov480218b2009-02-21 11:53:32 +0000908 }
909
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000910 if (Subtarget->isTargetDarwin()) {
911 SwitchToDataSection("");
Chris Lattner2e860262009-06-24 18:24:09 +0000912
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000913 // Add the (possibly multiple) personalities to the set of global value
914 // stubs. Only referenced functions get into the Personalities list.
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000915 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
Chris Lattner8b4c72c2009-06-24 18:17:00 +0000916 const std::vector<Function*> &Personalities = MMI->getPersonalities();
917 for (unsigned i = 0, e = Personalities.size(); i != e; ++i) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000918 if (Personalities[i])
919 GVStubs[Mang->getMangledName(Personalities[i], "$non_lazy_ptr",
920 true /*private label*/)] =
921 Mang->getMangledName(Personalities[i]);
Evan Cheng76443dc2008-07-08 00:55:58 +0000922 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000923 }
924
Chris Lattner2e860262009-06-24 18:24:09 +0000925 // Output stubs for dynamically-linked functions
926 if (!FnStubs.empty()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000927 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
928 "self_modifying_code+pure_instructions,5", 0);
929 for (StringMap<std::string>::iterator I = FnStubs.begin(),
930 E = FnStubs.end(); I != E; ++I)
931 O << I->getKeyData() << ":\n" << "\t.indirect_symbol " << I->second
932 << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000933 O << '\n';
934 }
935
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000936 // Output stubs for external and common global variables.
Chris Lattner2e860262009-06-24 18:24:09 +0000937 if (!GVStubs.empty()) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000938 SwitchToDataSection(
939 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000940 for (StringMap<std::string>::iterator I = GVStubs.begin(),
941 E = GVStubs.end(); I != E; ++I)
942 O << I->getKeyData() << ":\n\t.indirect_symbol "
943 << I->second << "\n\t.long\t0\n";
Chris Lattner2e860262009-06-24 18:24:09 +0000944 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000945
Evan Chenga65854f2008-12-05 01:06:39 +0000946 if (!HiddenGVStubs.empty()) {
947 SwitchToSection(TAI->getDataSection());
Chris Lattnerfadd47d2009-06-24 18:24:42 +0000948 EmitAlignment(2);
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000949 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
950 E = HiddenGVStubs.end(); I != E; ++I)
951 O << I->getKeyData() << ":\n" << TAI->getData32bitsDirective()
952 << I->second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000953 }
954
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000955 // Funny Darwin hack: This flag tells the linker that no global symbols
956 // contain code that falls through to other global symbols (e.g. the obvious
957 // implementation of multiple entry points). If this doesn't occur, the
958 // linker can safely perform dead code stripping. Since LLVM never
959 // generates code that does this, it is always safe to set.
960 O << "\t.subsections_via_symbols\n";
961 } else if (Subtarget->isTargetCygMing()) {
962 // Emit type information for external functions
Chris Lattner0f10ba62009-07-09 05:09:24 +0000963 for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000964 i != e; ++i) {
965 O << "\t.def\t " << i->getKeyData()
966 << ";\t.scl\t" << COFF::C_EXT
967 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
968 << ";\t.endef\n";
969 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000970 }
Chris Lattner5ec2b662009-06-24 05:47:59 +0000971
Chris Lattnerdb191f02009-06-24 18:52:01 +0000972
973 // Output linker support code for dllexported globals on windows.
974 if (!DLLExportedGVs.empty()) {
975 SwitchToDataSection(".section .drectve");
976
977 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
978 e = DLLExportedGVs.end(); i != e; ++i)
979 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
980 }
981
982 if (!DLLExportedFns.empty()) {
983 SwitchToDataSection(".section .drectve");
984
985 for (StringSet<>::iterator i = DLLExportedFns.begin(),
986 e = DLLExportedFns.end();
987 i != e; ++i)
988 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
989 }
990
Chris Lattnerdb191f02009-06-24 18:52:01 +0000991 // Do common shutdown.
992 bool Changed = AsmPrinter::doFinalization(M);
Chris Lattner5ec2b662009-06-24 05:47:59 +0000993
Chris Lattner7cf8daf2009-06-24 05:46:28 +0000994 if (NewAsmPrinter) {
995 Streamer->Finish();
996
997 delete Streamer;
998 delete Context;
999 Streamer = 0;
1000 Context = 0;
1001 }
1002
Chris Lattnerdb191f02009-06-24 18:52:01 +00001003 return Changed;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001004}
1005
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006// Include the auto-generated portion of the assembly writer.
1007#include "X86GenAsmWriter.inc"