blob: 51fc3bb3df18d0e640a2cfbe0954b62a3f6f89d7 [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"
18#include "X86.h"
Anton Korobeynikovd05ca652007-01-16 16:41:57 +000019#include "X86COFF.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000020#include "X86MachineFunctionInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000021#include "X86TargetMachine.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000022#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"
Jim Laskeya0f3d172006-09-07 22:06:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034
Chris Lattner95b2c7d2006-12-19 22:59:26 +000035STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
Evan Cheng0475ab52008-01-05 00:41:47 +000037static std::string getPICLabelString(unsigned FnNum,
38 const TargetAsmInfo *TAI,
39 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000040 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000041 if (Subtarget->isTargetDarwin())
Evan Cheng347d39f2007-10-14 05:57:21 +000042 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000043 else if (Subtarget->isTargetELF())
Dan Gohmand19a53b2008-06-30 22:03:41 +000044 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000045 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000046 assert(0 && "Don't know how to print PIC label!\n");
47
48 return label;
49}
50
Anton Korobeynikov75b68822008-06-28 11:08:27 +000051static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
52 const TargetData *TD) {
53 X86MachineFunctionInfo Info;
54 uint64_t Size = 0;
55
56 switch (F->getCallingConv()) {
57 case CallingConv::X86_StdCall:
58 Info.setDecorationStyle(StdCall);
59 break;
60 case CallingConv::X86_FastCall:
61 Info.setDecorationStyle(FastCall);
62 break;
63 default:
64 return Info;
65 }
66
67 unsigned argNum = 1;
68 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
69 AI != AE; ++AI, ++argNum) {
70 const Type* Ty = AI->getType();
71
72 // 'Dereference' type in case of byval parameter attribute
73 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
74 Ty = cast<PointerType>(Ty)->getElementType();
75
76 // Size should be aligned to DWORD boundary
77 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
78 }
79
80 // We're not supporting tooooo huge arguments :)
81 Info.setBytesToPopOnReturn((unsigned int)Size);
82 return Info;
83}
84
85/// PrintUnmangledNameSafely - Print out the printable characters in the name.
86/// Don't print things like \n or \0.
87static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
88 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
89 Name != E; ++Name)
90 if (isprint(*Name))
91 OS << *Name;
92}
93
94/// decorateName - Query FunctionInfoMap and use this information for various
95/// name decoration.
96void X86ATTAsmPrinter::decorateName(std::string &Name,
97 const GlobalValue *GV) {
98 const Function *F = dyn_cast<Function>(GV);
99 if (!F) return;
100
101 // We don't want to decorate non-stdcall or non-fastcall functions right now
102 unsigned CC = F->getCallingConv();
103 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
104 return;
105
106 // Decorate names only when we're targeting Cygwin/Mingw32 targets
107 if (!Subtarget->isTargetCygMing())
108 return;
109
110 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
111
112 const X86MachineFunctionInfo *Info;
113 if (info_item == FunctionInfoMap.end()) {
114 // Calculate apropriate function info and populate map
115 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
116 Info = &FunctionInfoMap[F];
117 } else {
118 Info = &info_item->second;
119 }
120
121 const FunctionType *FT = F->getFunctionType();
122 switch (Info->getDecorationStyle()) {
123 case None:
124 break;
125 case StdCall:
126 // "Pure" variadic functions do not receive @0 suffix.
127 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
128 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
129 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
130 break;
131 case FastCall:
132 // "Pure" variadic functions do not receive @0 suffix.
133 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
134 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
135 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
136
137 if (Name[0] == '_') {
138 Name[0] = '@';
139 } else {
140 Name = '@' + Name;
141 }
142 break;
143 default:
144 assert(0 && "Unsupported DecorationStyle");
145 }
146}
147
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000148// Substitute old hook with new one temporary
Chris Lattnerafbfded2006-10-05 02:43:52 +0000149std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000150 return TAI->SectionForGlobal(&F);
Chris Lattnerafbfded2006-10-05 02:43:52 +0000151}
152
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000153void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000154 const Function *F = MF.getFunction();
Anton Korobeynikovc5c92f62008-07-09 13:28:19 +0000155 std::string SectionName = TAI->SectionForGlobal(F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000156
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000157 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000158
Anton Korobeynikovc5c92f62008-07-09 13:28:19 +0000159 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikov91364382008-06-28 11:08:09 +0000160
Evan Chenge22e62b2008-03-25 22:29:46 +0000161 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000162 switch (F->getLinkage()) {
163 default: assert(0 && "Unknown linkage type!");
164 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000165 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000167 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000169 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000170 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000171 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000172 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000173 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000174 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000175 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000176 O << "\t.globl\t" << CurrentFnName << '\n';
177 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000178 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000179 O << "\t.globl\t" << CurrentFnName << "\n"
180 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000181 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000182 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000183 }
184 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000185 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000186 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000187 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000188 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000189 } else if (F->hasProtectedVisibility()) {
190 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000191 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000192 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000193
194 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000195 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000196 else if (Subtarget->isTargetCygMing()) {
197 O << "\t.def\t " << CurrentFnName
198 << ";\t.scl\t" <<
199 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
200 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
201 << ";\t.endef\n";
202 }
203
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000204 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000205 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000206 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000207 (F->getLinkage() == Function::LinkOnceLinkage ||
208 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000209 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000210}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000211
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000212/// runOnMachineFunction - This uses the printMachineInstruction()
213/// method to print assembly for each instruction.
214///
215bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
216 const Function *F = MF.getFunction();
217 unsigned CC = F->getCallingConv();
218
Evan Cheng526be702008-07-09 06:36:53 +0000219 if (TAI->doesSupportDebugInformation()) {
220 // Let PassManager know we need debug information and relay
221 // the MachineModuleInfo address on to DwarfWriter.
222 MMI = &getAnalysis<MachineModuleInfo>();
223 DW.SetModuleInfo(MMI);
224 }
225
Anton Korobeynikov1ecf0ee2008-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())
238 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
239
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())
245 DW.BeginFunction(&MF);
246
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000247 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000248 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000249 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
250 I != E; ++I) {
251 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000252 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000253 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000254 O << '\n';
255 }
Bill Wendling824a7212008-01-26 09:03:52 +0000256 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
257 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000258 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000259 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000260 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000261 printMachineInstruction(II);
262 }
263 }
Evan Cheng67afece2006-08-28 22:14:16 +0000264
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000265 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
266 // If the function is empty, then we need to emit *something*. Otherwise,
267 // the function's label might be associated with something that it wasn't
268 // meant to be associated with. We emit a noop in this situation.
269 // We are assuming inline asms are code.
270 O << "\tnop\n";
271 }
272
Jim Laskey563321a2006-09-06 18:34:40 +0000273 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000274 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000275
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000276 // Emit post-function debug information.
277 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000278 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000279
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000280 // Print out jump tables referenced by the function.
281 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000282
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000283 // We didn't modify anything.
284 return false;
285}
286
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000287static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000288 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
289}
290
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000291static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
292 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
293 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
294}
295
296static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000297 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
298}
299
Chris Lattnera3b8c572006-02-06 23:41:19 +0000300void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000301 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000302 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000303 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000304 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000305 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000306 "Virtual registers should not make it this far!");
307 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000308 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000309 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000310 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
312 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000313 Reg = getX86SubSuperRegister(Reg, VT);
314 }
Evan Chengae270f62008-07-07 22:21:06 +0000315 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000316 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000317 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000318
Chris Lattner63b3d712006-05-04 17:21:20 +0000319 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000320 if (!Modifier ||
321 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000322 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000323 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000324 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000325 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000326 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000327 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000328 case MachineOperand::MO_JumpTableIndex: {
329 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
330 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000331 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000332 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000333
334 if (TM.getRelocationModel() == Reloc::PIC_) {
335 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000336 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
337 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000338 else if (Subtarget->isPICStyleGOT())
339 O << "@GOTOFF";
340 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000341
Evan Chengae19abc2007-01-18 22:27:12 +0000342 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000343 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000344 return;
345 }
Evan Chenga09bd812006-02-26 08:28:12 +0000346 case MachineOperand::MO_ConstantPoolIndex: {
347 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
348 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000349 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000350 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000351
352 if (TM.getRelocationModel() == Reloc::PIC_) {
353 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000354 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
355 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000356 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000357 O << "@GOTOFF";
358 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000359
Evan Chenga09bd812006-02-26 08:28:12 +0000360 int Offset = MO.getOffset();
361 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000362 O << '+' << Offset;
Evan Chenga09bd812006-02-26 08:28:12 +0000363 else if (Offset < 0)
364 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000365
Evan Chengae19abc2007-01-18 22:27:12 +0000366 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000367 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000368 return;
369 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000370 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000371 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000372 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000373 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000374
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000375 const GlobalValue *GV = MO.getGlobal();
376 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
377 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000378 // If GV is an alias then use the aliasee for determining
379 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000380 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
381 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
382 }
383
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000384 bool isThreadLocal = GVar && GVar->isThreadLocal();
385
Evan Cheng25ab6902006-09-08 06:48:29 +0000386 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000387 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000388
Dan Gohman2a3250c2007-04-26 21:07:05 +0000389 if (!isMemOp && !isCallOp)
390 O << '$';
391 else if (Name[0] == '$') {
392 // The name begins with a dollar-sign. In order to avoid having it look
393 // like an integer immediate to the assembler, enclose it in parens.
394 O << '(';
395 needCloseParen = true;
396 }
397
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000398 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000399 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000400 // non-lazily-resolved stubs
Anton Korobeynikovc33a7442008-07-09 13:27:59 +0000401 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000402 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000404 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000405 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000406 } else {
407 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000408 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000409 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000410 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000411 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000412 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000413 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000414 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000415
Chris Lattner35d86fe2006-07-26 21:12:04 +0000416 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000417 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000418 } else {
419 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000420 O << "__imp_";
421 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000422 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000423
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000424 if (isCallOp) {
425 if (shouldPrintPLT(TM, Subtarget)) {
426 // Assemble call via PLT for externally visible symbols
427 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
428 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000429 O << "@PLT";
430 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000431 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000432 // Save function name for later type emission
433 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000434 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000435 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000436
Evan Cheng6d7d3102006-12-01 07:38:23 +0000437 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000438 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000439
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000440 int Offset = MO.getOffset();
441 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000442 O << '+' << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000443 else if (Offset < 0)
444 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000445
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000446 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000447 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000448 O << "@TLSGD"; // general dynamic TLS model
449 else
450 if (GV->isDeclaration())
451 O << "@INDNTPOFF"; // initial exec TLS model
452 else
453 O << "@NTPOFF"; // local exec TLS model
454 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000455 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000456 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000457 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000458 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000459 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000460 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
461 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000462 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000463 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000464
465 if (needCloseParen) {
466 needCloseParen = false;
467 O << ')';
468 }
469
Evan Chengae19abc2007-01-18 22:27:12 +0000470 // Use rip when possible to reduce code size, except when
471 // index or base register are also part of the address. e.g.
472 // foo(%rip)(%rcx,%rax,4) is not legal
473 O << "(%rip)";
474 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000475 }
476
Dan Gohman2a3250c2007-04-26 21:07:05 +0000477 if (needCloseParen)
478 O << ')';
479
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000480 return;
481 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000482 case MachineOperand::MO_ExternalSymbol: {
483 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000484 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000485 std::string Name(TAI->getGlobalPrefix());
486 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000487 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000488 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000489 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000490 return;
491 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000492 if (!isCallOp)
493 O << '$';
494 else if (Name[0] == '$') {
495 // The name begins with a dollar-sign. In order to avoid having it look
496 // like an integer immediate to the assembler, enclose it in parens.
497 O << '(';
498 needCloseParen = true;
499 }
500
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000501 O << Name;
502
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000503 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000504 std::string GOTName(TAI->getGlobalPrefix());
505 GOTName+="_GLOBAL_OFFSET_TABLE_";
506 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000507 // HACK! Emit extra offset to PC during printing GOT offset to
508 // compensate for the size of popl instruction. The resulting code
509 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000510 // call .piclabel
511 // piclabel:
512 // popl %some_register
513 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000514 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000515 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000516
517 if (isCallOp)
518 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000519 }
520
Dan Gohman2a3250c2007-04-26 21:07:05 +0000521 if (needCloseParen)
522 O << ')';
523
Evan Chengae19abc2007-01-18 22:27:12 +0000524 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000525 O << "(%rip)";
526
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000527 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000528 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000529 default:
530 O << "<unknown operand type>"; return;
531 }
532}
533
Nate Begeman391c5d22005-11-30 18:54:35 +0000534void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000535 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000536 assert(value <= 7 && "Invalid ssecc argument!");
537 switch (value) {
538 case 0: O << "eq"; break;
539 case 1: O << "lt"; break;
540 case 2: O << "le"; break;
541 case 3: O << "unord"; break;
542 case 4: O << "neq"; break;
543 case 5: O << "nlt"; break;
544 case 6: O << "nle"; break;
545 case 7: O << "ord"; break;
546 }
547}
548
Evan Cheng25ab6902006-09-08 06:48:29 +0000549void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
550 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000551 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000552 MachineOperand BaseReg = MI->getOperand(Op);
553 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000554 const MachineOperand &DispSpec = MI->getOperand(Op+3);
555
Evan Cheng28b514392006-12-05 19:50:18 +0000556 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000557 if (DispSpec.isGlobalAddress() ||
558 DispSpec.isConstantPoolIndex() ||
559 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000560 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000561 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000562 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000563 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
564 O << DispVal;
565 }
566
567 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000568 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000569 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000570
Chris Lattner32c9a452007-01-14 00:13:07 +0000571 // There are cases where we can end up with ESP/RSP in the indexreg slot.
572 // If this happens, swap the base/index register to support assemblers that
573 // don't work when the index is *SP.
574 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
575 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
576 std::swap(BaseReg, IndexReg);
577 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000578 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000579
Dan Gohmand19a53b2008-06-30 22:03:41 +0000580 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000581 if (BaseReg.getReg())
582 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000583
584 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000585 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000586 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000587 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000588 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000589 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000590 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000591 }
592}
593
Anton Korobeynikov91364382008-06-28 11:08:09 +0000594void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000595 const MachineBasicBlock *MBB) const {
596 if (!TAI->getSetDirective())
597 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000598
599 // We don't need .set machinery if we have GOT-style relocations
600 if (Subtarget->isPICStyleGOT())
601 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000602
Evan Chengcc415862007-11-09 01:32:10 +0000603 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
604 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000605 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000606 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000607 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000608 << '_' << uid << '\n';
609 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000610 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000611}
612
Evan Cheng7ccced62006-02-18 00:15:05 +0000613void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000614 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000615 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000616}
617
Evan Cheng62f27002006-04-28 23:11:40 +0000618
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000619void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
620 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000621 unsigned uid) const
622{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000623 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
624 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
625
626 O << JTEntryDirective << ' ';
627
628 if (TM.getRelocationModel() == Reloc::PIC_) {
629 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
630 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
631 << '_' << uid << "_set_" << MBB->getNumber();
632 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000633 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000634 O << "@GOTOFF";
635 } else
636 assert(0 && "Don't know how to print MBB label for this PIC mode");
637 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000638 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000639}
640
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000641bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000642 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000643 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000644 switch (Mode) {
645 default: return true; // Unknown mode.
646 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000647 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000648 break;
649 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000650 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000651 break;
652 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000653 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000654 break;
655 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000656 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000657 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000658 case 'q': // Print DImode register
659 Reg = getX86SubSuperRegister(Reg, MVT::i64);
660 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000661 }
662
Evan Chengae270f62008-07-07 22:21:06 +0000663 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000664 return false;
665}
666
Evan Cheng3d48a902006-04-28 21:19:05 +0000667/// PrintAsmOperand - Print out an operand for an inline asm expression.
668///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000669bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000670 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000671 const char *ExtraCode) {
672 // Does this asm operand have a single letter operand modifier?
673 if (ExtraCode && ExtraCode[0]) {
674 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000675
Evan Cheng3d48a902006-04-28 21:19:05 +0000676 switch (ExtraCode[0]) {
677 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000678 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000679 printOperand(MI, OpNo, "mem");
680 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000681 case 'b': // Print QImode register
682 case 'h': // Print QImode high register
683 case 'w': // Print HImode register
684 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000685 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000686 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000687 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
688 printOperand(MI, OpNo);
689 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000690
Chris Lattner7cd5e072007-03-25 01:44:57 +0000691 case 'P': // Don't print @PLT, but do print as memory.
692 printOperand(MI, OpNo, "mem");
693 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000694 }
695 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000696
Evan Cheng3d48a902006-04-28 21:19:05 +0000697 printOperand(MI, OpNo);
698 return false;
699}
700
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000701bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000702 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000703 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000704 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000705 if (ExtraCode && ExtraCode[0]) {
706 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000707
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000708 switch (ExtraCode[0]) {
709 default: return true; // Unknown modifier.
710 case 'b': // Print QImode register
711 case 'h': // Print QImode high register
712 case 'w': // Print HImode register
713 case 'k': // Print SImode register
714 case 'q': // Print SImode register
715 // These only apply to registers, ignore on mem.
716 break;
717 }
718 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000719 printMemReference(MI, OpNo);
720 return false;
721}
722
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000723/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000724/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000725///
726void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
727 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000728
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000729 // Call the autogenerated instruction printer routines.
730 printInstruction(MI);
731}
732
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000733/// doInitialization
734bool X86ATTAsmPrinter::doInitialization(Module &M) {
735 if (TAI->doesSupportDebugInformation()) {
736 // Emit initial debug information.
737 DW.BeginModule(&M);
738 }
739
Evan Cheng526be702008-07-09 06:36:53 +0000740 bool Result = AsmPrinter::doInitialization(M);
741
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000742 // Darwin wants symbols to be quoted if they have complex names.
743 if (Subtarget->isTargetDarwin())
744 Mang->setUseQuotes(true);
745
746 return Result;
747}
748
749
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000750void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000751 const TargetData *TD = TM.getTargetData();
752
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000753 if (!GVar->hasInitializer())
754 return; // External global require no code
755
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000756 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikov0e48a0c2008-07-09 13:24:38 +0000757
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000758 // Check to see if this is a special global used by LLVM, if so, emit it.
759 if (EmitSpecialLLVMGlobal(GVar)) {
760 if (Subtarget->isTargetDarwin() &&
761 TM.getRelocationModel() == Reloc::Static) {
762 if (GVar->getName() == "llvm.global_ctors")
763 O << ".reference .constructors_used\n";
764 else if (GVar->getName() == "llvm.global_dtors")
765 O << ".reference .destructors_used\n";
766 }
767 return;
768 }
769
770 std::string name = Mang->getValueName(GVar);
771 Constant *C = GVar->getInitializer();
772 const Type *Type = C->getType();
773 unsigned Size = TD->getABITypeSize(Type);
774 unsigned Align = TD->getPreferredAlignmentLog(GVar);
775
776 if (GVar->hasHiddenVisibility()) {
777 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000778 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000779 } else if (GVar->hasProtectedVisibility()) {
780 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000781 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000782 }
783
784 if (Subtarget->isTargetELF())
785 O << "\t.type\t" << name << ",@object\n";
786
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000787 SwitchToDataSection(SectionName.c_str());
788
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000789 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000790 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000791 if (GVar->hasExternalLinkage()) {
792 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000793 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000794 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000795 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000796 return;
797 }
798 }
799
800 if (!GVar->isThreadLocal() &&
Anton Korobeynikovc33a7442008-07-09 13:27:59 +0000801 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000802 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000803
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000804 if (TAI->getLCOMMDirective() != NULL) {
805 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000806 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000807 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000808 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000809 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000810 O << "\t.globl " << name << '\n'
811 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000812 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000813 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000814 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000815 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000816 EmitGlobalConstant(C);
817 return;
818 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000819 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000820
821 // Leopard and above support aligned common symbols.
822 if (Subtarget->getDarwinVers() >= 9)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000823 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000824 }
825 } else {
826 if (!Subtarget->isTargetCygMing()) {
827 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000828 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000829 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000830 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000831 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000832 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000834 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000836 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000837 return;
838 }
839 }
840
841 switch (GVar->getLinkage()) {
842 case GlobalValue::CommonLinkage:
843 case GlobalValue::LinkOnceLinkage:
844 case GlobalValue::WeakLinkage:
845 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000846 O << "\t.globl " << name << '\n'
847 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000849 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000850 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000851 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000852 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000853 }
854 break;
855 case GlobalValue::DLLExportLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 case GlobalValue::AppendingLinkage:
857 // FIXME: appending linkage variables should go into a section of
858 // their name or something. For now, just emit them as external.
859 case GlobalValue::ExternalLinkage:
860 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000861 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000862 // FALL THROUGH
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000863 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000864 break;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000865 default:
866 assert(0 && "Unknown linkage type!");
867 }
868
869 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000870 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000871 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000872 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000873 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000874 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000875
876 // If the initializer is a extern weak symbol, remember to emit the weak
877 // reference!
878 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
879 if (GV->hasExternalWeakLinkage())
880 ExtWeakSymbols.insert(GV);
881
882 EmitGlobalConstant(C);
883}
884
Evan Cheng77c8f762008-07-08 00:55:58 +0000885/// printGVStub - Print stub for a global value.
886///
887void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000888 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000889 O << ":\n\t.indirect_symbol ";
890 if (Prefix) O << Prefix;
891 O << GV << "\n\t.long\t0\n";
892}
893
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000894
895bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000896 // Print out module-level global variables here.
897 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000898 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000899 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000900
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000901 if (I->hasDLLExportLinkage())
902 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
903 }
904
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000905 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000906 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000907 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000908
909 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
910 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000911 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000912 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000913
914 if (!DLLExportedFns.empty()) {
915 SwitchToDataSection(".section .drectve");
916 }
917
918 for (StringSet<>::iterator i = DLLExportedFns.begin(),
919 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000920 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000921 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000922
923 if (Subtarget->isTargetDarwin()) {
924 SwitchToDataSection("");
925
926 // Output stubs for dynamically-linked functions
927 unsigned j = 1;
928 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
929 i != e; ++i, ++j) {
930 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
931 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +0000932 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000933 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +0000934 O << ":\n"
935 "\t.indirect_symbol " << p << "\n"
936 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000937 }
938
Dan Gohmand19a53b2008-06-30 22:03:41 +0000939 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000940
Evan Cheng77c8f762008-07-08 00:55:58 +0000941 // Print global value stubs.
942 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000943 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
944 // Add the (possibly multiple) personalities to the set of global values.
945 // Only referenced functions get into the Personalities list.
946 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000947 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +0000948 E = Personalities.end(); I != E; ++I) {
949 if (!*I)
950 continue;
951 if (!InStubSection) {
952 SwitchToDataSection(
953 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
954 InStubSection = true;
955 }
956 printGVStub((*I)->getNameStart(), "_");
957 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000958 }
959
960 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +0000961 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000962 SwitchToDataSection(
963 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
964 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +0000965 i != e; ++i)
966 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000967
968 // Emit final debug information.
969 DW.EndModule();
970
971 // Funny Darwin hack: This flag tells the linker that no global symbols
972 // contain code that falls through to other global symbols (e.g. the obvious
973 // implementation of multiple entry points). If this doesn't occur, the
974 // linker can safely perform dead code stripping. Since LLVM never
975 // generates code that does this, it is always safe to set.
976 O << "\t.subsections_via_symbols\n";
977 } else if (Subtarget->isTargetCygMing()) {
978 // Emit type information for external functions
979 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
980 i != e; ++i) {
981 O << "\t.def\t " << i->getKeyData()
982 << ";\t.scl\t" << COFF::C_EXT
983 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
984 << ";\t.endef\n";
985 }
986
987 // Emit final debug information.
988 DW.EndModule();
989 } else if (Subtarget->isTargetELF()) {
990 // Emit final debug information.
991 DW.EndModule();
992 }
993
994 return AsmPrinter::doFinalization(M);
995}
996
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000997// Include the auto-generated portion of the assembly writer.
998#include "X86GenAsmWriter.inc"