blob: 4e1c1dfde1d105436e67ffb3e630c546e29a1cfa [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
Chris Lattnerafbfded2006-10-05 02:43:52 +0000148/// getSectionForFunction - Return the section that we should emit the
149/// specified function body into.
150std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
151 switch (F.getLinkage()) {
152 default: assert(0 && "Unknown linkage type!");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000153 case Function::InternalLinkage:
Chris Lattnerafbfded2006-10-05 02:43:52 +0000154 case Function::DLLExportLinkage:
155 case Function::ExternalLinkage:
156 return TAI->getTextSection();
157 case Function::WeakLinkage:
158 case Function::LinkOnceLinkage:
159 if (Subtarget->isTargetDarwin()) {
160 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000161 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmanaf67ea72007-06-14 15:00:27 +0000162 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
Chris Lattnerafbfded2006-10-05 02:43:52 +0000163 } else {
164 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
Dan Gohmanaf67ea72007-06-14 15:00:27 +0000165 ",\"ax\",@progbits";
Chris Lattnerafbfded2006-10-05 02:43:52 +0000166 }
167 }
168}
169
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000170void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000171 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000172
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000173 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000174
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +0000175 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000176
Evan Chenge22e62b2008-03-25 22:29:46 +0000177 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000178 switch (F->getLinkage()) {
179 default: assert(0 && "Unknown linkage type!");
180 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000181 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000182 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000183 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000184 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000185 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000186 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000187 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000188 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000189 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000190 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000191 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000192 O << "\t.globl\t" << CurrentFnName << '\n';
193 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000194 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000195 O << "\t.globl\t" << CurrentFnName << "\n"
196 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000197 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000198 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000199 }
200 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000201 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000202 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000203 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000204 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000205 } else if (F->hasProtectedVisibility()) {
206 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000207 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000208 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000209
210 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000211 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000212 else if (Subtarget->isTargetCygMing()) {
213 O << "\t.def\t " << CurrentFnName
214 << ";\t.scl\t" <<
215 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
216 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
217 << ";\t.endef\n";
218 }
219
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000220 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000221 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000222 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000223 (F->getLinkage() == Function::LinkOnceLinkage ||
224 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000225 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000226}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000227
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000228/// runOnMachineFunction - This uses the printMachineInstruction()
229/// method to print assembly for each instruction.
230///
231bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
232 const Function *F = MF.getFunction();
233 unsigned CC = F->getCallingConv();
234
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000235 SetupMachineFunction(MF);
236 O << "\n\n";
237
238 // Populate function information map. Actually, We don't want to populate
239 // non-stdcall or non-fastcall functions' information right now.
240 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
241 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
242
243 // Print out constants referenced by the function
244 EmitConstantPool(MF.getConstantPool());
245
246 if (F->hasDLLExportLinkage())
247 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
248
249 // Print the 'header' of function
250 emitFunctionHeader(MF);
251
252 // Emit pre-function debug and/or EH information.
253 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
254 DW.BeginFunction(&MF);
255
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000256 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000257 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000258 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
259 I != E; ++I) {
260 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000261 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000262 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000263 O << '\n';
264 }
Bill Wendling824a7212008-01-26 09:03:52 +0000265 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
266 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000267 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000268 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000269 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000270 printMachineInstruction(II);
271 }
272 }
Evan Cheng67afece2006-08-28 22:14:16 +0000273
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000274 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
275 // If the function is empty, then we need to emit *something*. Otherwise,
276 // the function's label might be associated with something that it wasn't
277 // meant to be associated with. We emit a noop in this situation.
278 // We are assuming inline asms are code.
279 O << "\tnop\n";
280 }
281
Jim Laskey563321a2006-09-06 18:34:40 +0000282 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000283 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000284
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000285 // Emit post-function debug information.
286 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000287 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000288
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000289 // Print out jump tables referenced by the function.
290 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000291
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000292 // We didn't modify anything.
293 return false;
294}
295
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000296static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000297 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
298}
299
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000300static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
301 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
302 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
303}
304
305static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000306 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
307}
308
Chris Lattnera3b8c572006-02-06 23:41:19 +0000309void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000310 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000311 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000312 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000313 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000314 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000315 "Virtual registers should not make it this far!");
316 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000317 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000318 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000319 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
321 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000322 Reg = getX86SubSuperRegister(Reg, VT);
323 }
Evan Chengae270f62008-07-07 22:21:06 +0000324 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000325 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000326 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000327
Chris Lattner63b3d712006-05-04 17:21:20 +0000328 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000329 if (!Modifier ||
330 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000331 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000332 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000333 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000334 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000335 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000336 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000337 case MachineOperand::MO_JumpTableIndex: {
338 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
339 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000340 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000341 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000342
343 if (TM.getRelocationModel() == Reloc::PIC_) {
344 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000345 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
346 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000347 else if (Subtarget->isPICStyleGOT())
348 O << "@GOTOFF";
349 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000350
Evan Chengae19abc2007-01-18 22:27:12 +0000351 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000352 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000353 return;
354 }
Evan Chenga09bd812006-02-26 08:28:12 +0000355 case MachineOperand::MO_ConstantPoolIndex: {
356 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
357 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000358 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000359 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000360
361 if (TM.getRelocationModel() == Reloc::PIC_) {
362 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000363 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
364 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000365 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000366 O << "@GOTOFF";
367 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000368
Evan Chenga09bd812006-02-26 08:28:12 +0000369 int Offset = MO.getOffset();
370 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000371 O << '+' << Offset;
Evan Chenga09bd812006-02-26 08:28:12 +0000372 else if (Offset < 0)
373 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000374
Evan Chengae19abc2007-01-18 22:27:12 +0000375 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000376 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000377 return;
378 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000379 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000380 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000381 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000382 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000383
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000384 const GlobalValue *GV = MO.getGlobal();
385 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
386 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000387 // If GV is an alias then use the aliasee for determining
388 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000389 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
390 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
391 }
392
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000393 bool isThreadLocal = GVar && GVar->isThreadLocal();
394
Evan Cheng25ab6902006-09-08 06:48:29 +0000395 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000396 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000397
Dan Gohman2a3250c2007-04-26 21:07:05 +0000398 if (!isMemOp && !isCallOp)
399 O << '$';
400 else if (Name[0] == '$') {
401 // The name begins with a dollar-sign. In order to avoid having it look
402 // like an integer immediate to the assembler, enclose it in parens.
403 O << '(';
404 needCloseParen = true;
405 }
406
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000407 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000408 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000409 // non-lazily-resolved stubs
Reid Spencer5cbf9852007-01-30 20:08:39 +0000410 if (GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000411 GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000412 GV->hasLinkOnceLinkage() ||
413 GV->hasCommonLinkage()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000414 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000416 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000417 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000418 } else {
419 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000420 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000421 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000422 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000423 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000424 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000426 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000427
Chris Lattner35d86fe2006-07-26 21:12:04 +0000428 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000429 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000430 } else {
431 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000432 O << "__imp_";
433 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000434 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000435
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000436 if (isCallOp) {
437 if (shouldPrintPLT(TM, Subtarget)) {
438 // Assemble call via PLT for externally visible symbols
439 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
440 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000441 O << "@PLT";
442 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000443 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000444 // Save function name for later type emission
445 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000446 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000447 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000448
Evan Cheng6d7d3102006-12-01 07:38:23 +0000449 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000450 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000451
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000452 int Offset = MO.getOffset();
453 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000454 O << '+' << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000455 else if (Offset < 0)
456 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000457
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000458 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000459 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000460 O << "@TLSGD"; // general dynamic TLS model
461 else
462 if (GV->isDeclaration())
463 O << "@INDNTPOFF"; // initial exec TLS model
464 else
465 O << "@NTPOFF"; // local exec TLS model
466 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000467 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000468 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000469 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000470 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000471 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000472 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
473 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000474 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000475 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000476
477 if (needCloseParen) {
478 needCloseParen = false;
479 O << ')';
480 }
481
Evan Chengae19abc2007-01-18 22:27:12 +0000482 // Use rip when possible to reduce code size, except when
483 // index or base register are also part of the address. e.g.
484 // foo(%rip)(%rcx,%rax,4) is not legal
485 O << "(%rip)";
486 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000487 }
488
Dan Gohman2a3250c2007-04-26 21:07:05 +0000489 if (needCloseParen)
490 O << ')';
491
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000492 return;
493 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000494 case MachineOperand::MO_ExternalSymbol: {
495 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000496 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000497 std::string Name(TAI->getGlobalPrefix());
498 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000499 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000500 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000501 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000502 return;
503 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000504 if (!isCallOp)
505 O << '$';
506 else if (Name[0] == '$') {
507 // The name begins with a dollar-sign. In order to avoid having it look
508 // like an integer immediate to the assembler, enclose it in parens.
509 O << '(';
510 needCloseParen = true;
511 }
512
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000513 O << Name;
514
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000515 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000516 std::string GOTName(TAI->getGlobalPrefix());
517 GOTName+="_GLOBAL_OFFSET_TABLE_";
518 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000519 // HACK! Emit extra offset to PC during printing GOT offset to
520 // compensate for the size of popl instruction. The resulting code
521 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000522 // call .piclabel
523 // piclabel:
524 // popl %some_register
525 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000526 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000527 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000528
529 if (isCallOp)
530 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000531 }
532
Dan Gohman2a3250c2007-04-26 21:07:05 +0000533 if (needCloseParen)
534 O << ')';
535
Evan Chengae19abc2007-01-18 22:27:12 +0000536 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000537 O << "(%rip)";
538
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000539 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000540 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000541 default:
542 O << "<unknown operand type>"; return;
543 }
544}
545
Nate Begeman391c5d22005-11-30 18:54:35 +0000546void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000547 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000548 assert(value <= 7 && "Invalid ssecc argument!");
549 switch (value) {
550 case 0: O << "eq"; break;
551 case 1: O << "lt"; break;
552 case 2: O << "le"; break;
553 case 3: O << "unord"; break;
554 case 4: O << "neq"; break;
555 case 5: O << "nlt"; break;
556 case 6: O << "nle"; break;
557 case 7: O << "ord"; break;
558 }
559}
560
Evan Cheng25ab6902006-09-08 06:48:29 +0000561void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
562 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000563 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000564 MachineOperand BaseReg = MI->getOperand(Op);
565 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000566 const MachineOperand &DispSpec = MI->getOperand(Op+3);
567
Evan Cheng28b514392006-12-05 19:50:18 +0000568 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000569 if (DispSpec.isGlobalAddress() ||
570 DispSpec.isConstantPoolIndex() ||
571 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000572 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000573 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000574 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000575 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
576 O << DispVal;
577 }
578
579 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000580 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000581 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000582
Chris Lattner32c9a452007-01-14 00:13:07 +0000583 // There are cases where we can end up with ESP/RSP in the indexreg slot.
584 // If this happens, swap the base/index register to support assemblers that
585 // don't work when the index is *SP.
586 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
587 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
588 std::swap(BaseReg, IndexReg);
589 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000590 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000591
Dan Gohmand19a53b2008-06-30 22:03:41 +0000592 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000593 if (BaseReg.getReg())
594 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000595
596 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000597 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000598 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000599 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000600 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000601 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000602 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000603 }
604}
605
Anton Korobeynikov91364382008-06-28 11:08:09 +0000606void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000607 const MachineBasicBlock *MBB) const {
608 if (!TAI->getSetDirective())
609 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000610
611 // We don't need .set machinery if we have GOT-style relocations
612 if (Subtarget->isPICStyleGOT())
613 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000614
Evan Chengcc415862007-11-09 01:32:10 +0000615 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
616 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000617 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000618 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000619 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000620 << '_' << uid << '\n';
621 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000622 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000623}
624
Evan Cheng7ccced62006-02-18 00:15:05 +0000625void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000626 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000627 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000628}
629
Evan Cheng62f27002006-04-28 23:11:40 +0000630
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000631void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
632 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000633 unsigned uid) const
634{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000635 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
636 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
637
638 O << JTEntryDirective << ' ';
639
640 if (TM.getRelocationModel() == Reloc::PIC_) {
641 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
642 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
643 << '_' << uid << "_set_" << MBB->getNumber();
644 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000645 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000646 O << "@GOTOFF";
647 } else
648 assert(0 && "Don't know how to print MBB label for this PIC mode");
649 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000650 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000651}
652
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000653bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000654 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000655 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000656 switch (Mode) {
657 default: return true; // Unknown mode.
658 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000659 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000660 break;
661 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000662 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000663 break;
664 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000665 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000666 break;
667 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000668 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000669 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000670 case 'q': // Print DImode register
671 Reg = getX86SubSuperRegister(Reg, MVT::i64);
672 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000673 }
674
Evan Chengae270f62008-07-07 22:21:06 +0000675 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000676 return false;
677}
678
Evan Cheng3d48a902006-04-28 21:19:05 +0000679/// PrintAsmOperand - Print out an operand for an inline asm expression.
680///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000681bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000682 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000683 const char *ExtraCode) {
684 // Does this asm operand have a single letter operand modifier?
685 if (ExtraCode && ExtraCode[0]) {
686 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000687
Evan Cheng3d48a902006-04-28 21:19:05 +0000688 switch (ExtraCode[0]) {
689 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000690 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000691 printOperand(MI, OpNo, "mem");
692 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000693 case 'b': // Print QImode register
694 case 'h': // Print QImode high register
695 case 'w': // Print HImode register
696 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000697 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000698 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000699 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
700 printOperand(MI, OpNo);
701 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000702
Chris Lattner7cd5e072007-03-25 01:44:57 +0000703 case 'P': // Don't print @PLT, but do print as memory.
704 printOperand(MI, OpNo, "mem");
705 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000706 }
707 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000708
Evan Cheng3d48a902006-04-28 21:19:05 +0000709 printOperand(MI, OpNo);
710 return false;
711}
712
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000713bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000714 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000715 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000716 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000717 if (ExtraCode && ExtraCode[0]) {
718 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000719
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000720 switch (ExtraCode[0]) {
721 default: return true; // Unknown modifier.
722 case 'b': // Print QImode register
723 case 'h': // Print QImode high register
724 case 'w': // Print HImode register
725 case 'k': // Print SImode register
726 case 'q': // Print SImode register
727 // These only apply to registers, ignore on mem.
728 break;
729 }
730 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000731 printMemReference(MI, OpNo);
732 return false;
733}
734
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000735/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000736/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000737///
738void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
739 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000740
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000741 // Call the autogenerated instruction printer routines.
742 printInstruction(MI);
743}
744
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000745/// doInitialization
746bool X86ATTAsmPrinter::doInitialization(Module &M) {
Dale Johannesenf71cb9b2008-07-08 21:56:22 +0000747
748 bool Result = AsmPrinter::doInitialization(M);
749
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000750 if (TAI->doesSupportDebugInformation()) {
751 // Emit initial debug information.
752 DW.BeginModule(&M);
Dale Johannesenf71cb9b2008-07-08 21:56:22 +0000753 // Let PassManager know we need debug information and relay
754 // the MachineModuleInfo address on to DwarfWriter.
755 // AsmPrinter::doInitialization should have done this analysis.
756 MMI = getAnalysisToUpdate<MachineModuleInfo>();
757 assert(MMI);
758 DW.SetModuleInfo(MMI);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000759 }
760
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000761 // Darwin wants symbols to be quoted if they have complex names.
762 if (Subtarget->isTargetDarwin())
763 Mang->setUseQuotes(true);
764
765 return Result;
766}
767
768
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000769void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000770 const TargetData *TD = TM.getTargetData();
771
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000772 if (!GVar->hasInitializer())
773 return; // External global require no code
774
775 // Check to see if this is a special global used by LLVM, if so, emit it.
776 if (EmitSpecialLLVMGlobal(GVar)) {
777 if (Subtarget->isTargetDarwin() &&
778 TM.getRelocationModel() == Reloc::Static) {
779 if (GVar->getName() == "llvm.global_ctors")
780 O << ".reference .constructors_used\n";
781 else if (GVar->getName() == "llvm.global_dtors")
782 O << ".reference .destructors_used\n";
783 }
784 return;
785 }
786
787 std::string name = Mang->getValueName(GVar);
788 Constant *C = GVar->getInitializer();
789 const Type *Type = C->getType();
790 unsigned Size = TD->getABITypeSize(Type);
791 unsigned Align = TD->getPreferredAlignmentLog(GVar);
792
793 if (GVar->hasHiddenVisibility()) {
794 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000795 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000796 } else if (GVar->hasProtectedVisibility()) {
797 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000798 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000799 }
800
801 if (Subtarget->isTargetELF())
802 O << "\t.type\t" << name << ",@object\n";
803
804 if (C->isNullValue() && !GVar->hasSection()) {
805 if (GVar->hasExternalLinkage()) {
806 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000807 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000808 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000809 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000810 return;
811 }
812 }
813
814 if (!GVar->isThreadLocal() &&
815 (GVar->hasInternalLinkage() || GVar->hasWeakLinkage() ||
816 GVar->hasLinkOnceLinkage() || GVar->hasCommonLinkage())) {
817 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
818 if (!NoZerosInBSS && TAI->getBSSSection())
819 SwitchToDataSection(TAI->getBSSSection(), GVar);
820 else
821 SwitchToDataSection(TAI->getDataSection(), GVar);
822 if (TAI->getLCOMMDirective() != NULL) {
823 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000824 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000825 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000826 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000827 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000828 O << "\t.globl " << name << '\n'
829 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000830 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
831 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000832 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000834 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 EmitGlobalConstant(C);
836 return;
837 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000838 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000839
840 // Leopard and above support aligned common symbols.
841 if (Subtarget->getDarwinVers() >= 9)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000842 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000843 }
844 } else {
845 if (!Subtarget->isTargetCygMing()) {
846 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000847 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000849 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000853 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000854 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000855 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 return;
857 }
858 }
859
860 switch (GVar->getLinkage()) {
861 case GlobalValue::CommonLinkage:
862 case GlobalValue::LinkOnceLinkage:
863 case GlobalValue::WeakLinkage:
864 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000865 O << "\t.globl " << name << '\n'
866 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000867 if (!GVar->isConstant())
868 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
869 else {
870 const ArrayType *AT = dyn_cast<ArrayType>(Type);
871 if (AT && AT->getElementType()==Type::Int8Ty)
872 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", GVar);
873 else
874 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", GVar);
875 }
876 } else if (Subtarget->isTargetCygMing()) {
877 std::string SectionName(".section\t.data$linkonce." +
878 name +
879 ",\"aw\"");
880 SwitchToDataSection(SectionName.c_str(), GVar);
881 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000882 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000883 } else {
884 std::string SectionName("\t.section\t.llvm.linkonce.d." +
885 name +
886 ",\"aw\",@progbits");
887 SwitchToDataSection(SectionName.c_str(), GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000888 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000889 }
890 break;
891 case GlobalValue::DLLExportLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000892 case GlobalValue::AppendingLinkage:
893 // FIXME: appending linkage variables should go into a section of
894 // their name or something. For now, just emit them as external.
895 case GlobalValue::ExternalLinkage:
896 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000897 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000898 // FALL THROUGH
899 case GlobalValue::InternalLinkage: {
900 if (GVar->isConstant()) {
901 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
902 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
903 SwitchToDataSection(TAI->getCStringSection(), GVar);
904 break;
905 }
906 }
907 // FIXME: special handling for ".ctors" & ".dtors" sections
908 if (GVar->hasSection() &&
909 (GVar->getSection() == ".ctors" || GVar->getSection() == ".dtors")) {
910 std::string SectionName = ".section " + GVar->getSection();
911
912 if (Subtarget->isTargetCygMing()) {
913 SectionName += ",\"aw\"";
914 } else {
915 assert(!Subtarget->isTargetDarwin());
916 SectionName += ",\"aw\",@progbits";
917 }
918 SwitchToDataSection(SectionName.c_str());
919 } else if (GVar->hasSection() && Subtarget->isTargetDarwin()) {
920 // Honor all section names on Darwin; ObjC uses this
921 std::string SectionName = ".section " + GVar->getSection();
922 SwitchToDataSection(SectionName.c_str());
923 } else {
924 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
925 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSBSSSection() :
926 TAI->getBSSSection(), GVar);
927 else if (!GVar->isConstant())
928 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSDataSection() :
929 TAI->getDataSection(), GVar);
930 else if (GVar->isThreadLocal())
931 SwitchToDataSection(TAI->getTLSDataSection());
932 else {
933 // Read-only data.
934 bool HasReloc = C->ContainsRelocations();
935 if (HasReloc &&
936 Subtarget->isTargetDarwin() &&
937 TM.getRelocationModel() != Reloc::Static)
938 SwitchToDataSection("\t.const_data\n");
939 else if (!HasReloc && Size == 4 &&
940 TAI->getFourByteConstantSection())
941 SwitchToDataSection(TAI->getFourByteConstantSection(), GVar);
942 else if (!HasReloc && Size == 8 &&
943 TAI->getEightByteConstantSection())
944 SwitchToDataSection(TAI->getEightByteConstantSection(), GVar);
945 else if (!HasReloc && Size == 16 &&
946 TAI->getSixteenByteConstantSection())
947 SwitchToDataSection(TAI->getSixteenByteConstantSection(), GVar);
948 else if (TAI->getReadOnlySection())
949 SwitchToDataSection(TAI->getReadOnlySection(), GVar);
950 else
951 SwitchToDataSection(TAI->getDataSection(), GVar);
952 }
953 }
954
955 break;
956 }
957 default:
958 assert(0 && "Unknown linkage type!");
959 }
960
961 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000962 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000963 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000964 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000965 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000966 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000967
968 // If the initializer is a extern weak symbol, remember to emit the weak
969 // reference!
970 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
971 if (GV->hasExternalWeakLinkage())
972 ExtWeakSymbols.insert(GV);
973
974 EmitGlobalConstant(C);
975}
976
Evan Cheng77c8f762008-07-08 00:55:58 +0000977/// printGVStub - Print stub for a global value.
978///
979void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000980 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000981 O << ":\n\t.indirect_symbol ";
982 if (Prefix) O << Prefix;
983 O << GV << "\n\t.long\t0\n";
984}
985
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000986
987bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000988 // Print out module-level global variables here.
989 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000990 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000991 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000992
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000993 if (I->hasDLLExportLinkage())
994 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
995 }
996
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000997 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000998 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000999 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001000
1001 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1002 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001003 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001004 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001005
1006 if (!DLLExportedFns.empty()) {
1007 SwitchToDataSection(".section .drectve");
1008 }
1009
1010 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1011 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001012 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001013 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001014
1015 if (Subtarget->isTargetDarwin()) {
1016 SwitchToDataSection("");
1017
1018 // Output stubs for dynamically-linked functions
1019 unsigned j = 1;
1020 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1021 i != e; ++i, ++j) {
1022 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1023 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +00001024 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001025 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +00001026 O << ":\n"
1027 "\t.indirect_symbol " << p << "\n"
1028 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001029 }
1030
Dan Gohmand19a53b2008-06-30 22:03:41 +00001031 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001032
Evan Cheng77c8f762008-07-08 00:55:58 +00001033 // Print global value stubs.
1034 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001035 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1036 // Add the (possibly multiple) personalities to the set of global values.
1037 // Only referenced functions get into the Personalities list.
1038 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001039 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +00001040 E = Personalities.end(); I != E; ++I) {
1041 if (!*I)
1042 continue;
1043 if (!InStubSection) {
1044 SwitchToDataSection(
1045 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1046 InStubSection = true;
1047 }
1048 printGVStub((*I)->getNameStart(), "_");
1049 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001050 }
1051
1052 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +00001053 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001054 SwitchToDataSection(
1055 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1056 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +00001057 i != e; ++i)
1058 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001059
1060 // Emit final debug information.
1061 DW.EndModule();
1062
1063 // Funny Darwin hack: This flag tells the linker that no global symbols
1064 // contain code that falls through to other global symbols (e.g. the obvious
1065 // implementation of multiple entry points). If this doesn't occur, the
1066 // linker can safely perform dead code stripping. Since LLVM never
1067 // generates code that does this, it is always safe to set.
1068 O << "\t.subsections_via_symbols\n";
1069 } else if (Subtarget->isTargetCygMing()) {
1070 // Emit type information for external functions
1071 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1072 i != e; ++i) {
1073 O << "\t.def\t " << i->getKeyData()
1074 << ";\t.scl\t" << COFF::C_EXT
1075 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1076 << ";\t.endef\n";
1077 }
1078
1079 // Emit final debug information.
1080 DW.EndModule();
1081 } else if (Subtarget->isTargetELF()) {
1082 // Emit final debug information.
1083 DW.EndModule();
1084 }
1085
1086 return AsmPrinter::doFinalization(M);
1087}
1088
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001089// Include the auto-generated portion of the assembly writer.
1090#include "X86GenAsmWriter.inc"