blob: 66319c398161ef286079d5213ff04ab8b7dc4cdd [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
Cedric Venetd85f51a2008-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"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000032#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035
Chris Lattner95b2c7d2006-12-19 22:59:26 +000036STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
Evan Cheng0475ab52008-01-05 00:41:47 +000038static std::string getPICLabelString(unsigned FnNum,
39 const TargetAsmInfo *TAI,
40 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000041 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000042 if (Subtarget->isTargetDarwin())
Evan Cheng347d39f2007-10-14 05:57:21 +000043 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000044 else if (Subtarget->isTargetELF())
Dan Gohmand19a53b2008-06-30 22:03:41 +000045 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000046 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000047 assert(0 && "Don't know how to print PIC label!\n");
48
49 return label;
50}
51
Anton Korobeynikov75b68822008-06-28 11:08:27 +000052static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
53 const TargetData *TD) {
54 X86MachineFunctionInfo Info;
55 uint64_t Size = 0;
56
57 switch (F->getCallingConv()) {
58 case CallingConv::X86_StdCall:
59 Info.setDecorationStyle(StdCall);
60 break;
61 case CallingConv::X86_FastCall:
62 Info.setDecorationStyle(FastCall);
63 break;
64 default:
65 return Info;
66 }
67
68 unsigned argNum = 1;
69 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
70 AI != AE; ++AI, ++argNum) {
71 const Type* Ty = AI->getType();
72
73 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000074 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov75b68822008-06-28 11:08:27 +000075 Ty = cast<PointerType>(Ty)->getElementType();
76
77 // Size should be aligned to DWORD boundary
78 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
79 }
80
81 // We're not supporting tooooo huge arguments :)
82 Info.setBytesToPopOnReturn((unsigned int)Size);
83 return Info;
84}
85
86/// PrintUnmangledNameSafely - Print out the printable characters in the name.
87/// Don't print things like \n or \0.
Owen Andersoncb371882008-08-21 00:14:44 +000088static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +000089 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
90 Name != E; ++Name)
91 if (isprint(*Name))
92 OS << *Name;
93}
94
95/// decorateName - Query FunctionInfoMap and use this information for various
96/// name decoration.
97void X86ATTAsmPrinter::decorateName(std::string &Name,
98 const GlobalValue *GV) {
99 const Function *F = dyn_cast<Function>(GV);
100 if (!F) return;
101
102 // We don't want to decorate non-stdcall or non-fastcall functions right now
103 unsigned CC = F->getCallingConv();
104 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
105 return;
106
107 // Decorate names only when we're targeting Cygwin/Mingw32 targets
108 if (!Subtarget->isTargetCygMing())
109 return;
110
111 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
112
113 const X86MachineFunctionInfo *Info;
114 if (info_item == FunctionInfoMap.end()) {
115 // Calculate apropriate function info and populate map
116 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
117 Info = &FunctionInfoMap[F];
118 } else {
119 Info = &info_item->second;
120 }
121
122 const FunctionType *FT = F->getFunctionType();
123 switch (Info->getDecorationStyle()) {
124 case None:
125 break;
126 case StdCall:
127 // "Pure" variadic functions do not receive @0 suffix.
128 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
129 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
130 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
131 break;
132 case FastCall:
133 // "Pure" variadic functions do not receive @0 suffix.
134 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
135 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
137
138 if (Name[0] == '_') {
139 Name[0] = '@';
140 } else {
141 Name = '@' + Name;
142 }
143 break;
144 default:
145 assert(0 && "Unsupported DecorationStyle");
146 }
147}
148
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000149void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000150 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000151
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000152 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000153
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000154 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov91364382008-06-28 11:08:09 +0000155
Devang Patel4ae641f2008-10-01 23:18:38 +0000156 unsigned FnAlign = 4;
Devang Patele4d4b8c2008-10-06 17:30:07 +0000157 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Pateldb100332008-09-04 21:03:41 +0000158 FnAlign = 1;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000159 switch (F->getLinkage()) {
160 default: assert(0 && "Unknown linkage type!");
161 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000162 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000163 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000164 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000165 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000166 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000167 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000169 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000170 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000171 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000172 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000173 O << "\t.globl\t" << CurrentFnName << '\n';
174 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000175 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << "\n"
177 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000178 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000179 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000180 }
181 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000182 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000183
184 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000185
186 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000187 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000188 else if (Subtarget->isTargetCygMing()) {
189 O << "\t.def\t " << CurrentFnName
190 << ";\t.scl\t" <<
191 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
192 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
193 << ";\t.endef\n";
194 }
195
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000196 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000197 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000198 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000199 (F->getLinkage() == Function::LinkOnceLinkage ||
200 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000201 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000202}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000203
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000204/// runOnMachineFunction - This uses the printMachineInstruction()
205/// method to print assembly for each instruction.
206///
207bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
208 const Function *F = MF.getFunction();
209 unsigned CC = F->getCallingConv();
210
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000211 SetupMachineFunction(MF);
212 O << "\n\n";
213
214 // Populate function information map. Actually, We don't want to populate
215 // non-stdcall or non-fastcall functions' information right now.
216 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
217 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
218
219 // Print out constants referenced by the function
220 EmitConstantPool(MF.getConstantPool());
221
222 if (F->hasDLLExportLinkage())
223 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
224
225 // Print the 'header' of function
226 emitFunctionHeader(MF);
227
228 // Emit pre-function debug and/or EH information.
229 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
230 DW.BeginFunction(&MF);
231
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000232 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000233 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000234 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
235 I != E; ++I) {
236 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000237 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000238 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000239 O << '\n';
240 }
Bill Wendling824a7212008-01-26 09:03:52 +0000241 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
242 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000244 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000245 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000246 printMachineInstruction(II);
247 }
248 }
Evan Cheng67afece2006-08-28 22:14:16 +0000249
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000250 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
251 // If the function is empty, then we need to emit *something*. Otherwise,
252 // the function's label might be associated with something that it wasn't
253 // meant to be associated with. We emit a noop in this situation.
254 // We are assuming inline asms are code.
255 O << "\tnop\n";
256 }
257
Jim Laskey563321a2006-09-06 18:34:40 +0000258 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000259 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000261 // Emit post-function debug information.
262 if (TAI->doesSupportDebugInformation())
Bill Wendlingd751c642008-09-26 00:28:12 +0000263 DW.EndFunction(&MF);
Evan Cheng3c992d22006-03-07 02:02:57 +0000264
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000265 // Print out jump tables referenced by the function.
266 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000267
Dan Gohmane5f4de42008-11-07 19:49:17 +0000268 O.flush();
269
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000270 // We didn't modify anything.
271 return false;
272}
273
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000274static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000275 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
276}
277
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000278static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
279 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
280 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
281}
282
283static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000284 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
285}
286
Chris Lattnera3b8c572006-02-06 23:41:19 +0000287void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000288 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000289 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000290 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000291 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000292 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000293 "Virtual registers should not make it this far!");
294 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000295 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000296 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000297 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000298 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
299 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000300 Reg = getX86SubSuperRegister(Reg, VT);
301 }
Evan Chengae270f62008-07-07 22:21:06 +0000302 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000303 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000304 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000305
Chris Lattner63b3d712006-05-04 17:21:20 +0000306 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000307 if (!Modifier ||
308 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000309 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000310 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000311 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000312 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000313 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000314 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000315 case MachineOperand::MO_JumpTableIndex: {
316 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
317 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000318 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000319 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000320
321 if (TM.getRelocationModel() == Reloc::PIC_) {
322 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000323 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
324 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000325 else if (Subtarget->isPICStyleGOT())
326 O << "@GOTOFF";
327 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000328
Evan Chengae19abc2007-01-18 22:27:12 +0000329 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000330 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000331 return;
332 }
Evan Chenga09bd812006-02-26 08:28:12 +0000333 case MachineOperand::MO_ConstantPoolIndex: {
334 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
335 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000336 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000337 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000338
339 if (TM.getRelocationModel() == Reloc::PIC_) {
340 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000341 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
342 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000343 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000344 O << "@GOTOFF";
345 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000346
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000347 printOffset(MO.getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +0000348
Evan Chengae19abc2007-01-18 22:27:12 +0000349 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000350 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000351 return;
352 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000353 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000354 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000355 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000356 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000357
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000358 const GlobalValue *GV = MO.getGlobal();
359 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
360 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000361 // If GV is an alias then use the aliasee for determining
362 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000363 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000364 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000365 }
366
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000367 bool isThreadLocal = GVar && GVar->isThreadLocal();
368
Evan Cheng25ab6902006-09-08 06:48:29 +0000369 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000370 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000371
Dan Gohman2a3250c2007-04-26 21:07:05 +0000372 if (!isMemOp && !isCallOp)
373 O << '$';
374 else if (Name[0] == '$') {
375 // The name begins with a dollar-sign. In order to avoid having it look
376 // like an integer immediate to the assembler, enclose it in parens.
377 O << '(';
378 needCloseParen = true;
379 }
380
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000381 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000382 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000383 // non-lazily-resolved stubs
Duncan Sands5df31862008-09-29 11:25:42 +0000384 if (GV->isDeclaration() || GV->mayBeOverridden()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000385 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000386 if (isCallOp && isa<Function>(GV)) {
Evan Cheng91a23c82008-09-20 00:13:45 +0000387 // Function stubs are no longer needed for Mac OS X 10.5 and up.
388 if (Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9) {
389 O << Name;
390 } else {
391 FnStubs.insert(Name);
392 printSuffixedName(Name, "$stub");
393 }
Evan Cheng2338c5c2006-02-07 08:38:37 +0000394 } else {
395 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000396 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000397 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000398 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000399 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000400 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000401 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000402 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000403
Chris Lattner35d86fe2006-07-26 21:12:04 +0000404 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000405 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000406 } else {
407 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000408 O << "__imp_";
409 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000410 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000411
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000412 if (isCallOp) {
413 if (shouldPrintPLT(TM, Subtarget)) {
414 // Assemble call via PLT for externally visible symbols
415 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
416 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000417 O << "@PLT";
418 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000419 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000420 // Save function name for later type emission
421 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000422 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000423 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000424
Evan Cheng6d7d3102006-12-01 07:38:23 +0000425 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000426 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000427
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000428 printOffset(MO.getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +0000429
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000430 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000431 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000432 O << "@TLSGD"; // general dynamic TLS model
433 else
434 if (GV->isDeclaration())
435 O << "@INDNTPOFF"; // initial exec TLS model
436 else
437 O << "@NTPOFF"; // local exec TLS model
438 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000439 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000440 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000441 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000442 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000443 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000444 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
445 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000446 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000447 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000448
449 if (needCloseParen) {
450 needCloseParen = false;
451 O << ')';
452 }
453
Evan Chengae19abc2007-01-18 22:27:12 +0000454 // Use rip when possible to reduce code size, except when
455 // index or base register are also part of the address. e.g.
456 // foo(%rip)(%rcx,%rax,4) is not legal
457 O << "(%rip)";
458 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000459 }
460
Dan Gohman2a3250c2007-04-26 21:07:05 +0000461 if (needCloseParen)
462 O << ')';
463
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000464 return;
465 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000466 case MachineOperand::MO_ExternalSymbol: {
467 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000468 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000469 std::string Name(TAI->getGlobalPrefix());
470 Name += MO.getSymbolName();
Evan Cheng91a23c82008-09-20 00:13:45 +0000471 // Print function stub suffix unless it's Mac OS X 10.5 and up.
472 if (isCallOp && shouldPrintStub(TM, Subtarget) &&
473 !(Subtarget->isTargetDarwin() && Subtarget->getDarwinVers() >= 9)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000474 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000475 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000476 return;
477 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000478 if (!isCallOp)
479 O << '$';
480 else if (Name[0] == '$') {
481 // The name begins with a dollar-sign. In order to avoid having it look
482 // like an integer immediate to the assembler, enclose it in parens.
483 O << '(';
484 needCloseParen = true;
485 }
486
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000487 O << Name;
488
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000489 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000490 std::string GOTName(TAI->getGlobalPrefix());
491 GOTName+="_GLOBAL_OFFSET_TABLE_";
492 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000493 // HACK! Emit extra offset to PC during printing GOT offset to
494 // compensate for the size of popl instruction. The resulting code
495 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000496 // call .piclabel
497 // piclabel:
498 // popl %some_register
499 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000500 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000501 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000502
503 if (isCallOp)
504 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000505 }
506
Dan Gohman2a3250c2007-04-26 21:07:05 +0000507 if (needCloseParen)
508 O << ')';
509
Evan Chengae19abc2007-01-18 22:27:12 +0000510 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000511 O << "(%rip)";
512
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000513 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000514 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000515 default:
516 O << "<unknown operand type>"; return;
517 }
518}
519
Nate Begeman391c5d22005-11-30 18:54:35 +0000520void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000521 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000522 assert(value <= 7 && "Invalid ssecc argument!");
523 switch (value) {
524 case 0: O << "eq"; break;
525 case 1: O << "lt"; break;
526 case 2: O << "le"; break;
527 case 3: O << "unord"; break;
528 case 4: O << "neq"; break;
529 case 5: O << "nlt"; break;
530 case 6: O << "nle"; break;
531 case 7: O << "ord"; break;
532 }
533}
534
Evan Cheng25ab6902006-09-08 06:48:29 +0000535void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
536 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000537 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000538 MachineOperand BaseReg = MI->getOperand(Op);
539 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000540 const MachineOperand &DispSpec = MI->getOperand(Op+3);
541
Evan Cheng28b514392006-12-05 19:50:18 +0000542 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Dan Gohmand735b802008-10-03 15:45:36 +0000543 if (DispSpec.isGlobal() ||
544 DispSpec.isCPI() ||
545 DispSpec.isJTI()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000546 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000547 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000548 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000549 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
550 O << DispVal;
551 }
552
553 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000554 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000555 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000556
Chris Lattner32c9a452007-01-14 00:13:07 +0000557 // There are cases where we can end up with ESP/RSP in the indexreg slot.
558 // If this happens, swap the base/index register to support assemblers that
559 // don't work when the index is *SP.
560 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
561 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
562 std::swap(BaseReg, IndexReg);
563 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000564 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000565
Dan Gohmand19a53b2008-06-30 22:03:41 +0000566 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000567 if (BaseReg.getReg())
568 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000569
570 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000571 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000572 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000573 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000574 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000575 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000576 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000577 }
578}
579
Anton Korobeynikov91364382008-06-28 11:08:09 +0000580void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000581 const MachineBasicBlock *MBB) const {
582 if (!TAI->getSetDirective())
583 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000584
585 // We don't need .set machinery if we have GOT-style relocations
586 if (Subtarget->isPICStyleGOT())
587 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000588
Evan Chengcc415862007-11-09 01:32:10 +0000589 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
590 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000591 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000592 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000593 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000594 << '_' << uid << '\n';
595 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000596 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000597}
598
Evan Cheng7ccced62006-02-18 00:15:05 +0000599void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000600 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000601 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000602}
603
Evan Cheng62f27002006-04-28 23:11:40 +0000604
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000605void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
606 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000607 unsigned uid) const
608{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000609 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
610 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
611
612 O << JTEntryDirective << ' ';
613
614 if (TM.getRelocationModel() == Reloc::PIC_) {
615 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
616 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
617 << '_' << uid << "_set_" << MBB->getNumber();
618 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000619 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000620 O << "@GOTOFF";
621 } else
622 assert(0 && "Don't know how to print MBB label for this PIC mode");
623 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000624 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000625}
626
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000627bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000628 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000629 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000630 switch (Mode) {
631 default: return true; // Unknown mode.
632 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000633 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000634 break;
635 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000636 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000637 break;
638 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000639 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000640 break;
641 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000642 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000643 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000644 case 'q': // Print DImode register
645 Reg = getX86SubSuperRegister(Reg, MVT::i64);
646 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000647 }
648
Evan Chengae270f62008-07-07 22:21:06 +0000649 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000650 return false;
651}
652
Evan Cheng3d48a902006-04-28 21:19:05 +0000653/// PrintAsmOperand - Print out an operand for an inline asm expression.
654///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000655bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000656 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000657 const char *ExtraCode) {
658 // Does this asm operand have a single letter operand modifier?
659 if (ExtraCode && ExtraCode[0]) {
660 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000661
Evan Cheng3d48a902006-04-28 21:19:05 +0000662 switch (ExtraCode[0]) {
663 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000664 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000665 printOperand(MI, OpNo, "mem");
666 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000667 case 'b': // Print QImode register
668 case 'h': // Print QImode high register
669 case 'w': // Print HImode register
670 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000671 case 'q': // Print DImode register
Dan Gohmand735b802008-10-03 15:45:36 +0000672 if (MI->getOperand(OpNo).isReg())
Chris Lattner14393522007-03-25 02:01:03 +0000673 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
674 printOperand(MI, OpNo);
675 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000676
Chris Lattner7cd5e072007-03-25 01:44:57 +0000677 case 'P': // Don't print @PLT, but do print as memory.
678 printOperand(MI, OpNo, "mem");
679 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000680 }
681 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000682
Evan Cheng3d48a902006-04-28 21:19:05 +0000683 printOperand(MI, OpNo);
684 return false;
685}
686
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000687bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000688 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000689 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000690 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000691 if (ExtraCode && ExtraCode[0]) {
692 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000693
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000694 switch (ExtraCode[0]) {
695 default: return true; // Unknown modifier.
696 case 'b': // Print QImode register
697 case 'h': // Print QImode high register
698 case 'w': // Print HImode register
699 case 'k': // Print SImode register
700 case 'q': // Print SImode register
701 // These only apply to registers, ignore on mem.
702 break;
703 }
704 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000705 printMemReference(MI, OpNo);
706 return false;
707}
708
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000709/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000710/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000711///
712void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
713 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000714
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000715 // Call the autogenerated instruction printer routines.
716 printInstruction(MI);
717}
718
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000719/// doInitialization
720bool X86ATTAsmPrinter::doInitialization(Module &M) {
721 if (TAI->doesSupportDebugInformation()) {
722 // Emit initial debug information.
723 DW.BeginModule(&M);
724 }
725
Evan Cheng526be702008-07-09 06:36:53 +0000726 bool Result = AsmPrinter::doInitialization(M);
727
Dale Johannesen7bc39e22008-07-09 20:55:35 +0000728 if (TAI->doesSupportDebugInformation()) {
729 // Let PassManager know we need debug information and relay
730 // the MachineModuleInfo address on to DwarfWriter.
731 // AsmPrinter::doInitialization did this analysis.
732 MMI = getAnalysisToUpdate<MachineModuleInfo>();
733 DW.SetModuleInfo(MMI);
734 }
735
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000736 // Darwin wants symbols to be quoted if they have complex names.
737 if (Subtarget->isTargetDarwin())
738 Mang->setUseQuotes(true);
739
740 return Result;
741}
742
743
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000744void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000745 const TargetData *TD = TM.getTargetData();
746
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000747 if (!GVar->hasInitializer())
748 return; // External global require no code
749
750 // Check to see if this is a special global used by LLVM, if so, emit it.
751 if (EmitSpecialLLVMGlobal(GVar)) {
752 if (Subtarget->isTargetDarwin() &&
753 TM.getRelocationModel() == Reloc::Static) {
754 if (GVar->getName() == "llvm.global_ctors")
755 O << ".reference .constructors_used\n";
756 else if (GVar->getName() == "llvm.global_dtors")
757 O << ".reference .destructors_used\n";
758 }
759 return;
760 }
761
762 std::string name = Mang->getValueName(GVar);
763 Constant *C = GVar->getInitializer();
764 const Type *Type = C->getType();
765 unsigned Size = TD->getABITypeSize(Type);
766 unsigned Align = TD->getPreferredAlignmentLog(GVar);
767
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000768 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000769
770 if (Subtarget->isTargetELF())
771 O << "\t.type\t" << name << ",@object\n";
772
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000773 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000774
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000775 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000776 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000777 if (GVar->hasExternalLinkage()) {
778 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000779 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000780 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000781 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000782 return;
783 }
784 }
785
786 if (!GVar->isThreadLocal() &&
Duncan Sands5df31862008-09-29 11:25:42 +0000787 (GVar->hasInternalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000788 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000789
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000790 if (TAI->getLCOMMDirective() != NULL) {
791 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000792 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000793 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000794 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000795 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000796 O << "\t.globl " << name << '\n'
797 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000798 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000799 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000800 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000801 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000802 EmitGlobalConstant(C);
803 return;
804 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000805 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16b7f512008-08-08 18:25:52 +0000806 if (TAI->getCOMMDirectiveTakesAlignment())
807 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000808 }
809 } else {
810 if (!Subtarget->isTargetCygMing()) {
811 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000812 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000813 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000814 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000815 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000816 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000817 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000818 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000819 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000820 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000821 return;
822 }
823 }
824
825 switch (GVar->getLinkage()) {
Evan Cheng15621a22008-08-08 06:43:59 +0000826 case GlobalValue::CommonLinkage:
827 case GlobalValue::LinkOnceLinkage:
828 case GlobalValue::WeakLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000829 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000830 O << "\t.globl " << name << '\n'
831 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000832 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000834 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000836 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000837 }
838 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000839 case GlobalValue::DLLExportLinkage:
840 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000841 // FIXME: appending linkage variables should go into a section of
842 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000843 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000844 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000845 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000846 // FALL THROUGH
Evan Cheng15621a22008-08-08 06:43:59 +0000847 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000849 default:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850 assert(0 && "Unknown linkage type!");
851 }
852
853 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000854 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000855 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000856 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000857 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000858 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000859
860 // If the initializer is a extern weak symbol, remember to emit the weak
861 // reference!
862 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
863 if (GV->hasExternalWeakLinkage())
864 ExtWeakSymbols.insert(GV);
865
866 EmitGlobalConstant(C);
867}
868
Evan Cheng77c8f762008-07-08 00:55:58 +0000869/// printGVStub - Print stub for a global value.
870///
871void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000872 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000873 O << ":\n\t.indirect_symbol ";
874 if (Prefix) O << Prefix;
875 O << GV << "\n\t.long\t0\n";
876}
877
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000878
879bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000880 // Print out module-level global variables here.
881 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000882 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000883 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000884
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000885 if (I->hasDLLExportLinkage())
886 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
887 }
888
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000889 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000890 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000891 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000892
893 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
894 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000895 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000896 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000897
898 if (!DLLExportedFns.empty()) {
899 SwitchToDataSection(".section .drectve");
900 }
901
902 for (StringSet<>::iterator i = DLLExportedFns.begin(),
903 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000904 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000905 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000906
907 if (Subtarget->isTargetDarwin()) {
908 SwitchToDataSection("");
909
910 // Output stubs for dynamically-linked functions
911 unsigned j = 1;
912 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
913 i != e; ++i, ++j) {
914 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
915 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +0000916 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000917 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +0000918 O << ":\n"
919 "\t.indirect_symbol " << p << "\n"
920 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000921 }
922
Dan Gohmand19a53b2008-06-30 22:03:41 +0000923 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000924
Evan Cheng77c8f762008-07-08 00:55:58 +0000925 // Print global value stubs.
926 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000927 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
928 // Add the (possibly multiple) personalities to the set of global values.
929 // Only referenced functions get into the Personalities list.
930 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000931 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +0000932 E = Personalities.end(); I != E; ++I) {
933 if (!*I)
934 continue;
935 if (!InStubSection) {
936 SwitchToDataSection(
937 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
938 InStubSection = true;
939 }
940 printGVStub((*I)->getNameStart(), "_");
941 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000942 }
943
944 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +0000945 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000946 SwitchToDataSection(
947 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
948 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +0000949 i != e; ++i)
950 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000951
952 // Emit final debug information.
953 DW.EndModule();
954
955 // 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
963 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
964 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 }
970
971 // Emit final debug information.
972 DW.EndModule();
973 } else if (Subtarget->isTargetELF()) {
974 // Emit final debug information.
975 DW.EndModule();
976 }
977
978 return AsmPrinter::doFinalization(M);
979}
980
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000981// Include the auto-generated portion of the assembly writer.
982#include "X86GenAsmWriter.inc"