blob: eb31752ded984bc5eeb84239c6349bd4703b748e [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
Cedric Venetd85f51a2008-08-24 12:30:46 +000018#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000032#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035
Chris Lattner95b2c7d2006-12-19 22:59:26 +000036STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
Evan Cheng0475ab52008-01-05 00:41:47 +000038static std::string getPICLabelString(unsigned FnNum,
39 const TargetAsmInfo *TAI,
40 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000041 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000042 if (Subtarget->isTargetDarwin())
Evan Cheng347d39f2007-10-14 05:57:21 +000043 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000044 else if (Subtarget->isTargetELF())
Dan Gohmand19a53b2008-06-30 22:03:41 +000045 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000046 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000047 assert(0 && "Don't know how to print PIC label!\n");
48
49 return label;
50}
51
Anton Korobeynikov75b68822008-06-28 11:08:27 +000052static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
53 const TargetData *TD) {
54 X86MachineFunctionInfo Info;
55 uint64_t Size = 0;
56
57 switch (F->getCallingConv()) {
58 case CallingConv::X86_StdCall:
59 Info.setDecorationStyle(StdCall);
60 break;
61 case CallingConv::X86_FastCall:
62 Info.setDecorationStyle(FastCall);
63 break;
64 default:
65 return Info;
66 }
67
68 unsigned argNum = 1;
69 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
70 AI != AE; ++AI, ++argNum) {
71 const Type* Ty = AI->getType();
72
73 // 'Dereference' type in case of byval parameter attribute
74 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
75 Ty = cast<PointerType>(Ty)->getElementType();
76
77 // Size should be aligned to DWORD boundary
78 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
79 }
80
81 // We're not supporting tooooo huge arguments :)
82 Info.setBytesToPopOnReturn((unsigned int)Size);
83 return Info;
84}
85
86/// PrintUnmangledNameSafely - Print out the printable characters in the name.
87/// Don't print things like \n or \0.
Owen Andersoncb371882008-08-21 00:14:44 +000088static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +000089 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
90 Name != E; ++Name)
91 if (isprint(*Name))
92 OS << *Name;
93}
94
95/// decorateName - Query FunctionInfoMap and use this information for various
96/// name decoration.
97void X86ATTAsmPrinter::decorateName(std::string &Name,
98 const GlobalValue *GV) {
99 const Function *F = dyn_cast<Function>(GV);
100 if (!F) return;
101
102 // We don't want to decorate non-stdcall or non-fastcall functions right now
103 unsigned CC = F->getCallingConv();
104 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
105 return;
106
107 // Decorate names only when we're targeting Cygwin/Mingw32 targets
108 if (!Subtarget->isTargetCygMing())
109 return;
110
111 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
112
113 const X86MachineFunctionInfo *Info;
114 if (info_item == FunctionInfoMap.end()) {
115 // Calculate apropriate function info and populate map
116 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
117 Info = &FunctionInfoMap[F];
118 } else {
119 Info = &info_item->second;
120 }
121
122 const FunctionType *FT = F->getFunctionType();
123 switch (Info->getDecorationStyle()) {
124 case None:
125 break;
126 case StdCall:
127 // "Pure" variadic functions do not receive @0 suffix.
128 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
129 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
130 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
131 break;
132 case FastCall:
133 // "Pure" variadic functions do not receive @0 suffix.
134 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
135 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
136 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
137
138 if (Name[0] == '_') {
139 Name[0] = '@';
140 } else {
141 Name = '@' + Name;
142 }
143 break;
144 default:
145 assert(0 && "Unsupported DecorationStyle");
146 }
147}
148
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000149// Substitute old hook with new one temporary
Chris Lattnerafbfded2006-10-05 02:43:52 +0000150std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000151 return TAI->SectionForGlobal(&F);
Chris Lattnerafbfded2006-10-05 02:43:52 +0000152}
153
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000154void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000155 const Function *F = MF.getFunction();
Anton Korobeynikovc5c92f62008-07-09 13:28:19 +0000156 std::string SectionName = TAI->SectionForGlobal(F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000157
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000158 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000159
Anton Korobeynikovc5c92f62008-07-09 13:28:19 +0000160 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikov91364382008-06-28 11:08:09 +0000161
Evan Chenge22e62b2008-03-25 22:29:46 +0000162 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Devang Pateldb100332008-09-04 21:03:41 +0000163 if (FnAlign == 4 && (F->getNotes() & FN_NOTE_OptimizeForSize))
164 FnAlign = 1;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000165 switch (F->getLinkage()) {
166 default: assert(0 && "Unknown linkage type!");
167 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000168 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000169 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000170 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000171 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000172 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000173 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000174 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000175 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000176 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000177 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000178 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000179 O << "\t.globl\t" << CurrentFnName << '\n';
180 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000181 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000182 O << "\t.globl\t" << CurrentFnName << "\n"
183 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000184 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000185 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000186 }
187 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000188 }
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000189
190 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000191
192 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000193 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000194 else if (Subtarget->isTargetCygMing()) {
195 O << "\t.def\t " << CurrentFnName
196 << ";\t.scl\t" <<
197 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
198 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
199 << ";\t.endef\n";
200 }
201
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000202 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000203 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000204 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000205 (F->getLinkage() == Function::LinkOnceLinkage ||
206 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000207 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000208}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000210/// runOnMachineFunction - This uses the printMachineInstruction()
211/// method to print assembly for each instruction.
212///
213bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
214 const Function *F = MF.getFunction();
215 unsigned CC = F->getCallingConv();
216
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000217 SetupMachineFunction(MF);
218 O << "\n\n";
219
220 // Populate function information map. Actually, We don't want to populate
221 // non-stdcall or non-fastcall functions' information right now.
222 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
223 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
224
225 // Print out constants referenced by the function
226 EmitConstantPool(MF.getConstantPool());
227
228 if (F->hasDLLExportLinkage())
229 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
230
231 // Print the 'header' of function
232 emitFunctionHeader(MF);
233
234 // Emit pre-function debug and/or EH information.
235 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
236 DW.BeginFunction(&MF);
237
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000238 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000239 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000240 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
241 I != E; ++I) {
242 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000243 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000244 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000245 O << '\n';
246 }
Bill Wendling824a7212008-01-26 09:03:52 +0000247 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
248 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000249 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000250 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000251 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000252 printMachineInstruction(II);
253 }
254 }
Evan Cheng67afece2006-08-28 22:14:16 +0000255
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000256 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
257 // If the function is empty, then we need to emit *something*. Otherwise,
258 // the function's label might be associated with something that it wasn't
259 // meant to be associated with. We emit a noop in this situation.
260 // We are assuming inline asms are code.
261 O << "\tnop\n";
262 }
263
Jim Laskey563321a2006-09-06 18:34:40 +0000264 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000265 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000266
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000267 // Emit post-function debug information.
268 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000269 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000270
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000271 // Print out jump tables referenced by the function.
272 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000273
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274 // We didn't modify anything.
275 return false;
276}
277
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000278static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000279 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
280}
281
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000282static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
283 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
284 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
285}
286
287static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000288 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
289}
290
Chris Lattnera3b8c572006-02-06 23:41:19 +0000291void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000292 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000293 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000294 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000295 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000296 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000297 "Virtual registers should not make it this far!");
298 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000299 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000300 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000301 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000302 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
303 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000304 Reg = getX86SubSuperRegister(Reg, VT);
305 }
Evan Chengae270f62008-07-07 22:21:06 +0000306 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000307 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000308 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000309
Chris Lattner63b3d712006-05-04 17:21:20 +0000310 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000311 if (!Modifier ||
312 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000313 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000314 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000315 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000316 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000317 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000318 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000319 case MachineOperand::MO_JumpTableIndex: {
320 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
321 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000322 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000323 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000324
325 if (TM.getRelocationModel() == Reloc::PIC_) {
326 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000327 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
328 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000329 else if (Subtarget->isPICStyleGOT())
330 O << "@GOTOFF";
331 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000332
Evan Chengae19abc2007-01-18 22:27:12 +0000333 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000334 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000335 return;
336 }
Evan Chenga09bd812006-02-26 08:28:12 +0000337 case MachineOperand::MO_ConstantPoolIndex: {
338 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
339 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000340 O << TAI->getPrivateGlobalPrefix() << "CPI" << 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\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000347 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000348 O << "@GOTOFF";
349 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000350
Evan Chenga09bd812006-02-26 08:28:12 +0000351 int Offset = MO.getOffset();
352 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000353 O << '+' << Offset;
Evan Chenga09bd812006-02-26 08:28:12 +0000354 else if (Offset < 0)
355 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000356
Evan Chengae19abc2007-01-18 22:27:12 +0000357 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000358 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000359 return;
360 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000361 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000362 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000363 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000364 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000365
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000366 const GlobalValue *GV = MO.getGlobal();
367 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
368 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000369 // If GV is an alias then use the aliasee for determining
370 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000371 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
372 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
373 }
374
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000375 bool isThreadLocal = GVar && GVar->isThreadLocal();
376
Evan Cheng25ab6902006-09-08 06:48:29 +0000377 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000378 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000379
Dan Gohman2a3250c2007-04-26 21:07:05 +0000380 if (!isMemOp && !isCallOp)
381 O << '$';
382 else if (Name[0] == '$') {
383 // The name begins with a dollar-sign. In order to avoid having it look
384 // like an integer immediate to the assembler, enclose it in parens.
385 O << '(';
386 needCloseParen = true;
387 }
388
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000389 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000390 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000391 // non-lazily-resolved stubs
Anton Korobeynikovc33a7442008-07-09 13:27:59 +0000392 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000393 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000394 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000395 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000396 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000397 } else {
398 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000399 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000400 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000401 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000402 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000403 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000404 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000405 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000406
Chris Lattner35d86fe2006-07-26 21:12:04 +0000407 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000408 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000409 } else {
410 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000411 O << "__imp_";
412 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000413 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000414
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000415 if (isCallOp) {
416 if (shouldPrintPLT(TM, Subtarget)) {
417 // Assemble call via PLT for externally visible symbols
418 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
419 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000420 O << "@PLT";
421 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000422 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000423 // Save function name for later type emission
424 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000425 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000426 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000427
Evan Cheng6d7d3102006-12-01 07:38:23 +0000428 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000429 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000430
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000431 int Offset = MO.getOffset();
432 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000433 O << '+' << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000434 else if (Offset < 0)
435 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000436
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000437 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000438 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000439 O << "@TLSGD"; // general dynamic TLS model
440 else
441 if (GV->isDeclaration())
442 O << "@INDNTPOFF"; // initial exec TLS model
443 else
444 O << "@NTPOFF"; // local exec TLS model
445 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000446 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000447 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000448 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000449 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000450 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000451 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
452 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000453 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000454 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000455
456 if (needCloseParen) {
457 needCloseParen = false;
458 O << ')';
459 }
460
Evan Chengae19abc2007-01-18 22:27:12 +0000461 // Use rip when possible to reduce code size, except when
462 // index or base register are also part of the address. e.g.
463 // foo(%rip)(%rcx,%rax,4) is not legal
464 O << "(%rip)";
465 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000466 }
467
Dan Gohman2a3250c2007-04-26 21:07:05 +0000468 if (needCloseParen)
469 O << ')';
470
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000471 return;
472 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000473 case MachineOperand::MO_ExternalSymbol: {
474 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000475 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000476 std::string Name(TAI->getGlobalPrefix());
477 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000478 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000479 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000480 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000481 return;
482 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000483 if (!isCallOp)
484 O << '$';
485 else if (Name[0] == '$') {
486 // The name begins with a dollar-sign. In order to avoid having it look
487 // like an integer immediate to the assembler, enclose it in parens.
488 O << '(';
489 needCloseParen = true;
490 }
491
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000492 O << Name;
493
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000494 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000495 std::string GOTName(TAI->getGlobalPrefix());
496 GOTName+="_GLOBAL_OFFSET_TABLE_";
497 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000498 // HACK! Emit extra offset to PC during printing GOT offset to
499 // compensate for the size of popl instruction. The resulting code
500 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000501 // call .piclabel
502 // piclabel:
503 // popl %some_register
504 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000505 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000506 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000507
508 if (isCallOp)
509 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000510 }
511
Dan Gohman2a3250c2007-04-26 21:07:05 +0000512 if (needCloseParen)
513 O << ')';
514
Evan Chengae19abc2007-01-18 22:27:12 +0000515 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000516 O << "(%rip)";
517
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000518 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000519 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000520 default:
521 O << "<unknown operand type>"; return;
522 }
523}
524
Nate Begeman391c5d22005-11-30 18:54:35 +0000525void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000526 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000527 assert(value <= 7 && "Invalid ssecc argument!");
528 switch (value) {
529 case 0: O << "eq"; break;
530 case 1: O << "lt"; break;
531 case 2: O << "le"; break;
532 case 3: O << "unord"; break;
533 case 4: O << "neq"; break;
534 case 5: O << "nlt"; break;
535 case 6: O << "nle"; break;
536 case 7: O << "ord"; break;
537 }
538}
539
Evan Cheng25ab6902006-09-08 06:48:29 +0000540void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
541 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000542 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000543 MachineOperand BaseReg = MI->getOperand(Op);
544 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000545 const MachineOperand &DispSpec = MI->getOperand(Op+3);
546
Evan Cheng28b514392006-12-05 19:50:18 +0000547 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000548 if (DispSpec.isGlobalAddress() ||
549 DispSpec.isConstantPoolIndex() ||
550 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000551 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000552 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000553 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000554 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
555 O << DispVal;
556 }
557
558 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000559 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000560 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000561
Chris Lattner32c9a452007-01-14 00:13:07 +0000562 // There are cases where we can end up with ESP/RSP in the indexreg slot.
563 // If this happens, swap the base/index register to support assemblers that
564 // don't work when the index is *SP.
565 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
566 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
567 std::swap(BaseReg, IndexReg);
568 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000569 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000570
Dan Gohmand19a53b2008-06-30 22:03:41 +0000571 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000572 if (BaseReg.getReg())
573 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000574
575 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000576 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000577 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000578 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000579 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000580 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000581 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000582 }
583}
584
Anton Korobeynikov91364382008-06-28 11:08:09 +0000585void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000586 const MachineBasicBlock *MBB) const {
587 if (!TAI->getSetDirective())
588 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000589
590 // We don't need .set machinery if we have GOT-style relocations
591 if (Subtarget->isPICStyleGOT())
592 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000593
Evan Chengcc415862007-11-09 01:32:10 +0000594 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
595 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000596 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000597 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000598 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000599 << '_' << uid << '\n';
600 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000601 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000602}
603
Evan Cheng7ccced62006-02-18 00:15:05 +0000604void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000605 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000606 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000607}
608
Evan Cheng62f27002006-04-28 23:11:40 +0000609
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000610void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
611 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000612 unsigned uid) const
613{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000614 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
615 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
616
617 O << JTEntryDirective << ' ';
618
619 if (TM.getRelocationModel() == Reloc::PIC_) {
620 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
621 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
622 << '_' << uid << "_set_" << MBB->getNumber();
623 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000624 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000625 O << "@GOTOFF";
626 } else
627 assert(0 && "Don't know how to print MBB label for this PIC mode");
628 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000629 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000630}
631
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000632bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000633 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000634 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000635 switch (Mode) {
636 default: return true; // Unknown mode.
637 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000638 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000639 break;
640 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000641 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000642 break;
643 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000644 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000645 break;
646 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000647 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000648 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000649 case 'q': // Print DImode register
650 Reg = getX86SubSuperRegister(Reg, MVT::i64);
651 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000652 }
653
Evan Chengae270f62008-07-07 22:21:06 +0000654 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000655 return false;
656}
657
Evan Cheng3d48a902006-04-28 21:19:05 +0000658/// PrintAsmOperand - Print out an operand for an inline asm expression.
659///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000660bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000661 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000662 const char *ExtraCode) {
663 // Does this asm operand have a single letter operand modifier?
664 if (ExtraCode && ExtraCode[0]) {
665 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000666
Evan Cheng3d48a902006-04-28 21:19:05 +0000667 switch (ExtraCode[0]) {
668 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000669 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000670 printOperand(MI, OpNo, "mem");
671 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000672 case 'b': // Print QImode register
673 case 'h': // Print QImode high register
674 case 'w': // Print HImode register
675 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000676 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000677 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000678 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
679 printOperand(MI, OpNo);
680 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000681
Chris Lattner7cd5e072007-03-25 01:44:57 +0000682 case 'P': // Don't print @PLT, but do print as memory.
683 printOperand(MI, OpNo, "mem");
684 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000685 }
686 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000687
Evan Cheng3d48a902006-04-28 21:19:05 +0000688 printOperand(MI, OpNo);
689 return false;
690}
691
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000692bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000693 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000694 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000695 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000696 if (ExtraCode && ExtraCode[0]) {
697 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000698
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000699 switch (ExtraCode[0]) {
700 default: return true; // Unknown modifier.
701 case 'b': // Print QImode register
702 case 'h': // Print QImode high register
703 case 'w': // Print HImode register
704 case 'k': // Print SImode register
705 case 'q': // Print SImode register
706 // These only apply to registers, ignore on mem.
707 break;
708 }
709 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000710 printMemReference(MI, OpNo);
711 return false;
712}
713
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000714/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000715/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000716///
717void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
718 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000719
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000720 // Call the autogenerated instruction printer routines.
721 printInstruction(MI);
722}
723
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000724/// doInitialization
725bool X86ATTAsmPrinter::doInitialization(Module &M) {
726 if (TAI->doesSupportDebugInformation()) {
727 // Emit initial debug information.
728 DW.BeginModule(&M);
729 }
730
Evan Cheng526be702008-07-09 06:36:53 +0000731 bool Result = AsmPrinter::doInitialization(M);
732
Dale Johannesen7bc39e22008-07-09 20:55:35 +0000733 if (TAI->doesSupportDebugInformation()) {
734 // Let PassManager know we need debug information and relay
735 // the MachineModuleInfo address on to DwarfWriter.
736 // AsmPrinter::doInitialization did this analysis.
737 MMI = getAnalysisToUpdate<MachineModuleInfo>();
738 DW.SetModuleInfo(MMI);
739 }
740
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000741 // Darwin wants symbols to be quoted if they have complex names.
742 if (Subtarget->isTargetDarwin())
743 Mang->setUseQuotes(true);
744
745 return Result;
746}
747
748
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000749void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000750 const TargetData *TD = TM.getTargetData();
751
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000752 if (!GVar->hasInitializer())
753 return; // External global require no code
754
755 // Check to see if this is a special global used by LLVM, if so, emit it.
756 if (EmitSpecialLLVMGlobal(GVar)) {
757 if (Subtarget->isTargetDarwin() &&
758 TM.getRelocationModel() == Reloc::Static) {
759 if (GVar->getName() == "llvm.global_ctors")
760 O << ".reference .constructors_used\n";
761 else if (GVar->getName() == "llvm.global_dtors")
762 O << ".reference .destructors_used\n";
763 }
764 return;
765 }
766
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000767 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000768 std::string name = Mang->getValueName(GVar);
769 Constant *C = GVar->getInitializer();
770 const Type *Type = C->getType();
771 unsigned Size = TD->getABITypeSize(Type);
772 unsigned Align = TD->getPreferredAlignmentLog(GVar);
773
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000774 printVisibility(name, GVar->getVisibility());
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000775
776 if (Subtarget->isTargetELF())
777 O << "\t.type\t" << name << ",@object\n";
778
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000779 SwitchToDataSection(SectionName.c_str());
780
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000781 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000782 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000783 if (GVar->hasExternalLinkage()) {
784 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000785 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000786 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000787 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000788 return;
789 }
790 }
791
792 if (!GVar->isThreadLocal() &&
Anton Korobeynikovc33a7442008-07-09 13:27:59 +0000793 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000794 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000795
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000796 if (TAI->getLCOMMDirective() != NULL) {
797 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000798 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000799 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000800 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000801 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000802 O << "\t.globl " << name << '\n'
803 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000804 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000805 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000806 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000807 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000808 EmitGlobalConstant(C);
809 return;
810 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000811 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikov16b7f512008-08-08 18:25:52 +0000812 if (TAI->getCOMMDirectiveTakesAlignment())
813 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000814 }
815 } else {
816 if (!Subtarget->isTargetCygMing()) {
817 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000818 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000819 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000820 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000821 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000822 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000823 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000824 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000825 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000826 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000827 return;
828 }
829 }
830
831 switch (GVar->getLinkage()) {
Evan Cheng15621a22008-08-08 06:43:59 +0000832 case GlobalValue::CommonLinkage:
833 case GlobalValue::LinkOnceLinkage:
834 case GlobalValue::WeakLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000836 O << "\t.globl " << name << '\n'
837 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000838 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000839 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000840 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000841 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000842 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000843 }
844 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000845 case GlobalValue::DLLExportLinkage:
846 case GlobalValue::AppendingLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000847 // FIXME: appending linkage variables should go into a section of
848 // their name or something. For now, just emit them as external.
Evan Cheng15621a22008-08-08 06:43:59 +0000849 case GlobalValue::ExternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852 // FALL THROUGH
Evan Cheng15621a22008-08-08 06:43:59 +0000853 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000854 break;
Evan Cheng15621a22008-08-08 06:43:59 +0000855 default:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 assert(0 && "Unknown linkage type!");
857 }
858
859 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000860 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000861 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000862 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000863 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000864 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000865
866 // If the initializer is a extern weak symbol, remember to emit the weak
867 // reference!
868 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
869 if (GV->hasExternalWeakLinkage())
870 ExtWeakSymbols.insert(GV);
871
872 EmitGlobalConstant(C);
873}
874
Evan Cheng77c8f762008-07-08 00:55:58 +0000875/// printGVStub - Print stub for a global value.
876///
877void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000878 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000879 O << ":\n\t.indirect_symbol ";
880 if (Prefix) O << Prefix;
881 O << GV << "\n\t.long\t0\n";
882}
883
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000884
885bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000886 // Print out module-level global variables here.
887 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000888 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000889 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000890
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000891 if (I->hasDLLExportLinkage())
892 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
893 }
894
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000895 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000896 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000897 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000898
899 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
900 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000901 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000902 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000903
904 if (!DLLExportedFns.empty()) {
905 SwitchToDataSection(".section .drectve");
906 }
907
908 for (StringSet<>::iterator i = DLLExportedFns.begin(),
909 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000910 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000911 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000912
913 if (Subtarget->isTargetDarwin()) {
914 SwitchToDataSection("");
915
916 // Output stubs for dynamically-linked functions
917 unsigned j = 1;
918 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
919 i != e; ++i, ++j) {
920 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
921 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +0000922 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000923 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +0000924 O << ":\n"
925 "\t.indirect_symbol " << p << "\n"
926 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000927 }
928
Dan Gohmand19a53b2008-06-30 22:03:41 +0000929 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000930
Evan Cheng77c8f762008-07-08 00:55:58 +0000931 // Print global value stubs.
932 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000933 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
934 // Add the (possibly multiple) personalities to the set of global values.
935 // Only referenced functions get into the Personalities list.
936 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000937 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +0000938 E = Personalities.end(); I != E; ++I) {
939 if (!*I)
940 continue;
941 if (!InStubSection) {
942 SwitchToDataSection(
943 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
944 InStubSection = true;
945 }
946 printGVStub((*I)->getNameStart(), "_");
947 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000948 }
949
950 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +0000951 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000952 SwitchToDataSection(
953 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
954 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +0000955 i != e; ++i)
956 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000957
958 // Emit final debug information.
959 DW.EndModule();
960
961 // Funny Darwin hack: This flag tells the linker that no global symbols
962 // contain code that falls through to other global symbols (e.g. the obvious
963 // implementation of multiple entry points). If this doesn't occur, the
964 // linker can safely perform dead code stripping. Since LLVM never
965 // generates code that does this, it is always safe to set.
966 O << "\t.subsections_via_symbols\n";
967 } else if (Subtarget->isTargetCygMing()) {
968 // Emit type information for external functions
969 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
970 i != e; ++i) {
971 O << "\t.def\t " << i->getKeyData()
972 << ";\t.scl\t" << COFF::C_EXT
973 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
974 << ";\t.endef\n";
975 }
976
977 // Emit final debug information.
978 DW.EndModule();
979 } else if (Subtarget->isTargetELF()) {
980 // Emit final debug information.
981 DW.EndModule();
982 }
983
984 return AsmPrinter::doFinalization(M);
985}
986
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000987// Include the auto-generated portion of the assembly writer.
988#include "X86GenAsmWriter.inc"