blob: 61556baea1b13d7d9e3413574bf8dd695e8f94de [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
Anton Korobeynikov0e48a0c2008-07-09 13:24:38 +000035#include <iostream>
36
Chris Lattner95b2c7d2006-12-19 22:59:26 +000037STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Evan Cheng0475ab52008-01-05 00:41:47 +000039static std::string getPICLabelString(unsigned FnNum,
40 const TargetAsmInfo *TAI,
41 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000042 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000043 if (Subtarget->isTargetDarwin())
Evan Cheng347d39f2007-10-14 05:57:21 +000044 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000045 else if (Subtarget->isTargetELF())
Dan Gohmand19a53b2008-06-30 22:03:41 +000046 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000047 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000048 assert(0 && "Don't know how to print PIC label!\n");
49
50 return label;
51}
52
Anton Korobeynikov75b68822008-06-28 11:08:27 +000053static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
54 const TargetData *TD) {
55 X86MachineFunctionInfo Info;
56 uint64_t Size = 0;
57
58 switch (F->getCallingConv()) {
59 case CallingConv::X86_StdCall:
60 Info.setDecorationStyle(StdCall);
61 break;
62 case CallingConv::X86_FastCall:
63 Info.setDecorationStyle(FastCall);
64 break;
65 default:
66 return Info;
67 }
68
69 unsigned argNum = 1;
70 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
71 AI != AE; ++AI, ++argNum) {
72 const Type* Ty = AI->getType();
73
74 // 'Dereference' type in case of byval parameter attribute
75 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
76 Ty = cast<PointerType>(Ty)->getElementType();
77
78 // Size should be aligned to DWORD boundary
79 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
80 }
81
82 // We're not supporting tooooo huge arguments :)
83 Info.setBytesToPopOnReturn((unsigned int)Size);
84 return Info;
85}
86
87/// PrintUnmangledNameSafely - Print out the printable characters in the name.
88/// Don't print things like \n or \0.
89static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
90 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
91 Name != E; ++Name)
92 if (isprint(*Name))
93 OS << *Name;
94}
95
96/// decorateName - Query FunctionInfoMap and use this information for various
97/// name decoration.
98void X86ATTAsmPrinter::decorateName(std::string &Name,
99 const GlobalValue *GV) {
100 const Function *F = dyn_cast<Function>(GV);
101 if (!F) return;
102
103 // We don't want to decorate non-stdcall or non-fastcall functions right now
104 unsigned CC = F->getCallingConv();
105 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
106 return;
107
108 // Decorate names only when we're targeting Cygwin/Mingw32 targets
109 if (!Subtarget->isTargetCygMing())
110 return;
111
112 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
113
114 const X86MachineFunctionInfo *Info;
115 if (info_item == FunctionInfoMap.end()) {
116 // Calculate apropriate function info and populate map
117 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
118 Info = &FunctionInfoMap[F];
119 } else {
120 Info = &info_item->second;
121 }
122
123 const FunctionType *FT = F->getFunctionType();
124 switch (Info->getDecorationStyle()) {
125 case None:
126 break;
127 case StdCall:
128 // "Pure" variadic functions do not receive @0 suffix.
129 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
131 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
132 break;
133 case FastCall:
134 // "Pure" variadic functions do not receive @0 suffix.
135 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
137 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
138
139 if (Name[0] == '_') {
140 Name[0] = '@';
141 } else {
142 Name = '@' + Name;
143 }
144 break;
145 default:
146 assert(0 && "Unsupported DecorationStyle");
147 }
148}
149
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000150// Substitute old hook with new one temporary
Chris Lattnerafbfded2006-10-05 02:43:52 +0000151std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000152 return TAI->SectionForGlobal(&F);
Chris Lattnerafbfded2006-10-05 02:43:52 +0000153}
154
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000155void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000156 const Function *F = MF.getFunction();
Anton Korobeynikovc5c92f62008-07-09 13:28:19 +0000157 std::string SectionName = TAI->SectionForGlobal(F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000158
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000159 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000160
Anton Korobeynikovc5c92f62008-07-09 13:28:19 +0000161 SwitchToTextSection(SectionName.c_str());
Anton Korobeynikov91364382008-06-28 11:08:09 +0000162
Evan Chenge22e62b2008-03-25 22:29:46 +0000163 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 switch (F->getLinkage()) {
165 default: assert(0 && "Unknown linkage type!");
166 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000167 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000169 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000170 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000171 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000172 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000173 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000174 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000175 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000176 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000177 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000178 O << "\t.globl\t" << CurrentFnName << '\n';
179 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000180 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000181 O << "\t.globl\t" << CurrentFnName << "\n"
182 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000183 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000184 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000185 }
186 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000187 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000188 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000189 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000190 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000191 } else if (F->hasProtectedVisibility()) {
192 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000193 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000194 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000195
196 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000197 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000198 else if (Subtarget->isTargetCygMing()) {
199 O << "\t.def\t " << CurrentFnName
200 << ";\t.scl\t" <<
201 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
202 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
203 << ";\t.endef\n";
204 }
205
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000206 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000207 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000208 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000209 (F->getLinkage() == Function::LinkOnceLinkage ||
210 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000211 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000212}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000214/// runOnMachineFunction - This uses the printMachineInstruction()
215/// method to print assembly for each instruction.
216///
217bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
218 const Function *F = MF.getFunction();
219 unsigned CC = F->getCallingConv();
220
Evan Cheng526be702008-07-09 06:36:53 +0000221 if (TAI->doesSupportDebugInformation()) {
222 // Let PassManager know we need debug information and relay
223 // the MachineModuleInfo address on to DwarfWriter.
224 MMI = &getAnalysis<MachineModuleInfo>();
225 DW.SetModuleInfo(MMI);
226 }
227
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000228 SetupMachineFunction(MF);
229 O << "\n\n";
230
231 // Populate function information map. Actually, We don't want to populate
232 // non-stdcall or non-fastcall functions' information right now.
233 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
234 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
235
236 // Print out constants referenced by the function
237 EmitConstantPool(MF.getConstantPool());
238
239 if (F->hasDLLExportLinkage())
240 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
241
242 // Print the 'header' of function
243 emitFunctionHeader(MF);
244
245 // Emit pre-function debug and/or EH information.
246 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
247 DW.BeginFunction(&MF);
248
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000249 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000250 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000251 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
252 I != E; ++I) {
253 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000254 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000255 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000256 O << '\n';
257 }
Bill Wendling824a7212008-01-26 09:03:52 +0000258 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
259 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000261 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000262 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000263 printMachineInstruction(II);
264 }
265 }
Evan Cheng67afece2006-08-28 22:14:16 +0000266
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000267 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
268 // If the function is empty, then we need to emit *something*. Otherwise,
269 // the function's label might be associated with something that it wasn't
270 // meant to be associated with. We emit a noop in this situation.
271 // We are assuming inline asms are code.
272 O << "\tnop\n";
273 }
274
Jim Laskey563321a2006-09-06 18:34:40 +0000275 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000276 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000277
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000278 // Emit post-function debug information.
279 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000280 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000281
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000282 // Print out jump tables referenced by the function.
283 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000284
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000285 // We didn't modify anything.
286 return false;
287}
288
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000289static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000290 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
291}
292
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000293static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
294 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
295 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
296}
297
298static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000299 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
300}
301
Chris Lattnera3b8c572006-02-06 23:41:19 +0000302void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000303 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000304 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000305 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000306 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000307 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000308 "Virtual registers should not make it this far!");
309 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000310 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000311 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000312 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
314 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000315 Reg = getX86SubSuperRegister(Reg, VT);
316 }
Evan Chengae270f62008-07-07 22:21:06 +0000317 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000318 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000319 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000320
Chris Lattner63b3d712006-05-04 17:21:20 +0000321 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000322 if (!Modifier ||
323 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000324 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000325 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000326 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000327 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000328 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000329 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000330 case MachineOperand::MO_JumpTableIndex: {
331 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
332 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000333 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000334 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000335
336 if (TM.getRelocationModel() == Reloc::PIC_) {
337 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000338 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
339 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000340 else if (Subtarget->isPICStyleGOT())
341 O << "@GOTOFF";
342 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000343
Evan Chengae19abc2007-01-18 22:27:12 +0000344 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000345 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000346 return;
347 }
Evan Chenga09bd812006-02-26 08:28:12 +0000348 case MachineOperand::MO_ConstantPoolIndex: {
349 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
350 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000351 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000352 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000353
354 if (TM.getRelocationModel() == Reloc::PIC_) {
355 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000356 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
357 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000358 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000359 O << "@GOTOFF";
360 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000361
Evan Chenga09bd812006-02-26 08:28:12 +0000362 int Offset = MO.getOffset();
363 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000364 O << '+' << Offset;
Evan Chenga09bd812006-02-26 08:28:12 +0000365 else if (Offset < 0)
366 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000367
Evan Chengae19abc2007-01-18 22:27:12 +0000368 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000369 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000370 return;
371 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000372 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000373 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000374 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000375 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000376
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000377 const GlobalValue *GV = MO.getGlobal();
378 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
379 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000380 // If GV is an alias then use the aliasee for determining
381 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000382 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
383 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
384 }
385
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000386 bool isThreadLocal = GVar && GVar->isThreadLocal();
387
Evan Cheng25ab6902006-09-08 06:48:29 +0000388 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000389 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000390
Dan Gohman2a3250c2007-04-26 21:07:05 +0000391 if (!isMemOp && !isCallOp)
392 O << '$';
393 else if (Name[0] == '$') {
394 // The name begins with a dollar-sign. In order to avoid having it look
395 // like an integer immediate to the assembler, enclose it in parens.
396 O << '(';
397 needCloseParen = true;
398 }
399
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000400 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000401 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000402 // non-lazily-resolved stubs
Anton Korobeynikovc33a7442008-07-09 13:27:59 +0000403 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000404 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000405 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000406 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000407 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000408 } else {
409 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000410 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000411 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000412 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000413 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000414 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000416 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000417
Chris Lattner35d86fe2006-07-26 21:12:04 +0000418 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000419 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000420 } else {
421 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000422 O << "__imp_";
423 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000425
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000426 if (isCallOp) {
427 if (shouldPrintPLT(TM, Subtarget)) {
428 // Assemble call via PLT for externally visible symbols
429 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
430 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000431 O << "@PLT";
432 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000433 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000434 // Save function name for later type emission
435 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000436 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000437 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000438
Evan Cheng6d7d3102006-12-01 07:38:23 +0000439 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000440 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000441
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000442 int Offset = MO.getOffset();
443 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000444 O << '+' << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000445 else if (Offset < 0)
446 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000447
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000448 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000449 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000450 O << "@TLSGD"; // general dynamic TLS model
451 else
452 if (GV->isDeclaration())
453 O << "@INDNTPOFF"; // initial exec TLS model
454 else
455 O << "@NTPOFF"; // local exec TLS model
456 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000457 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000458 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000459 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000460 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000461 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000462 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
463 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000464 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000465 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000466
467 if (needCloseParen) {
468 needCloseParen = false;
469 O << ')';
470 }
471
Evan Chengae19abc2007-01-18 22:27:12 +0000472 // Use rip when possible to reduce code size, except when
473 // index or base register are also part of the address. e.g.
474 // foo(%rip)(%rcx,%rax,4) is not legal
475 O << "(%rip)";
476 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000477 }
478
Dan Gohman2a3250c2007-04-26 21:07:05 +0000479 if (needCloseParen)
480 O << ')';
481
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000482 return;
483 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000484 case MachineOperand::MO_ExternalSymbol: {
485 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000486 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000487 std::string Name(TAI->getGlobalPrefix());
488 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000489 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000490 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000491 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000492 return;
493 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000494 if (!isCallOp)
495 O << '$';
496 else if (Name[0] == '$') {
497 // The name begins with a dollar-sign. In order to avoid having it look
498 // like an integer immediate to the assembler, enclose it in parens.
499 O << '(';
500 needCloseParen = true;
501 }
502
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000503 O << Name;
504
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000505 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000506 std::string GOTName(TAI->getGlobalPrefix());
507 GOTName+="_GLOBAL_OFFSET_TABLE_";
508 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000509 // HACK! Emit extra offset to PC during printing GOT offset to
510 // compensate for the size of popl instruction. The resulting code
511 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000512 // call .piclabel
513 // piclabel:
514 // popl %some_register
515 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000516 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000517 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000518
519 if (isCallOp)
520 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000521 }
522
Dan Gohman2a3250c2007-04-26 21:07:05 +0000523 if (needCloseParen)
524 O << ')';
525
Evan Chengae19abc2007-01-18 22:27:12 +0000526 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000527 O << "(%rip)";
528
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000529 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000530 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000531 default:
532 O << "<unknown operand type>"; return;
533 }
534}
535
Nate Begeman391c5d22005-11-30 18:54:35 +0000536void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000537 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000538 assert(value <= 7 && "Invalid ssecc argument!");
539 switch (value) {
540 case 0: O << "eq"; break;
541 case 1: O << "lt"; break;
542 case 2: O << "le"; break;
543 case 3: O << "unord"; break;
544 case 4: O << "neq"; break;
545 case 5: O << "nlt"; break;
546 case 6: O << "nle"; break;
547 case 7: O << "ord"; break;
548 }
549}
550
Evan Cheng25ab6902006-09-08 06:48:29 +0000551void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
552 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000553 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000554 MachineOperand BaseReg = MI->getOperand(Op);
555 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000556 const MachineOperand &DispSpec = MI->getOperand(Op+3);
557
Evan Cheng28b514392006-12-05 19:50:18 +0000558 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000559 if (DispSpec.isGlobalAddress() ||
560 DispSpec.isConstantPoolIndex() ||
561 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000562 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000563 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000564 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000565 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
566 O << DispVal;
567 }
568
569 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000570 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000571 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000572
Chris Lattner32c9a452007-01-14 00:13:07 +0000573 // There are cases where we can end up with ESP/RSP in the indexreg slot.
574 // If this happens, swap the base/index register to support assemblers that
575 // don't work when the index is *SP.
576 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
577 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
578 std::swap(BaseReg, IndexReg);
579 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000580 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000581
Dan Gohmand19a53b2008-06-30 22:03:41 +0000582 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000583 if (BaseReg.getReg())
584 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000585
586 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000587 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000588 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000589 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000590 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000591 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000592 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000593 }
594}
595
Anton Korobeynikov91364382008-06-28 11:08:09 +0000596void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000597 const MachineBasicBlock *MBB) const {
598 if (!TAI->getSetDirective())
599 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000600
601 // We don't need .set machinery if we have GOT-style relocations
602 if (Subtarget->isPICStyleGOT())
603 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000604
Evan Chengcc415862007-11-09 01:32:10 +0000605 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
606 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000607 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000608 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000609 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000610 << '_' << uid << '\n';
611 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000612 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000613}
614
Evan Cheng7ccced62006-02-18 00:15:05 +0000615void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000616 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000617 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000618}
619
Evan Cheng62f27002006-04-28 23:11:40 +0000620
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000621void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
622 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000623 unsigned uid) const
624{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000625 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
626 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
627
628 O << JTEntryDirective << ' ';
629
630 if (TM.getRelocationModel() == Reloc::PIC_) {
631 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
632 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
633 << '_' << uid << "_set_" << MBB->getNumber();
634 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000635 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000636 O << "@GOTOFF";
637 } else
638 assert(0 && "Don't know how to print MBB label for this PIC mode");
639 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000640 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000641}
642
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000643bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000644 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000645 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000646 switch (Mode) {
647 default: return true; // Unknown mode.
648 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000649 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000650 break;
651 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000652 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000653 break;
654 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000655 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000656 break;
657 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000658 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000659 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000660 case 'q': // Print DImode register
661 Reg = getX86SubSuperRegister(Reg, MVT::i64);
662 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000663 }
664
Evan Chengae270f62008-07-07 22:21:06 +0000665 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000666 return false;
667}
668
Evan Cheng3d48a902006-04-28 21:19:05 +0000669/// PrintAsmOperand - Print out an operand for an inline asm expression.
670///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000671bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000672 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000673 const char *ExtraCode) {
674 // Does this asm operand have a single letter operand modifier?
675 if (ExtraCode && ExtraCode[0]) {
676 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000677
Evan Cheng3d48a902006-04-28 21:19:05 +0000678 switch (ExtraCode[0]) {
679 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000680 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000681 printOperand(MI, OpNo, "mem");
682 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000683 case 'b': // Print QImode register
684 case 'h': // Print QImode high register
685 case 'w': // Print HImode register
686 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000687 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000688 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000689 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
690 printOperand(MI, OpNo);
691 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000692
Chris Lattner7cd5e072007-03-25 01:44:57 +0000693 case 'P': // Don't print @PLT, but do print as memory.
694 printOperand(MI, OpNo, "mem");
695 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000696 }
697 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000698
Evan Cheng3d48a902006-04-28 21:19:05 +0000699 printOperand(MI, OpNo);
700 return false;
701}
702
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000703bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000704 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000705 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000706 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000707 if (ExtraCode && ExtraCode[0]) {
708 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000709
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000710 switch (ExtraCode[0]) {
711 default: return true; // Unknown modifier.
712 case 'b': // Print QImode register
713 case 'h': // Print QImode high register
714 case 'w': // Print HImode register
715 case 'k': // Print SImode register
716 case 'q': // Print SImode register
717 // These only apply to registers, ignore on mem.
718 break;
719 }
720 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000721 printMemReference(MI, OpNo);
722 return false;
723}
724
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000725/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000726/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000727///
728void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
729 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000730
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000731 // Call the autogenerated instruction printer routines.
732 printInstruction(MI);
733}
734
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000735/// doInitialization
736bool X86ATTAsmPrinter::doInitialization(Module &M) {
737 if (TAI->doesSupportDebugInformation()) {
738 // Emit initial debug information.
739 DW.BeginModule(&M);
740 }
741
Evan Cheng526be702008-07-09 06:36:53 +0000742 bool Result = AsmPrinter::doInitialization(M);
743
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000744 // Darwin wants symbols to be quoted if they have complex names.
745 if (Subtarget->isTargetDarwin())
746 Mang->setUseQuotes(true);
747
748 return Result;
749}
750
751
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000752void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000753 const TargetData *TD = TM.getTargetData();
754
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000755 if (!GVar->hasInitializer())
756 return; // External global require no code
757
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000758 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikov0e48a0c2008-07-09 13:24:38 +0000759
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000760 // Check to see if this is a special global used by LLVM, if so, emit it.
761 if (EmitSpecialLLVMGlobal(GVar)) {
762 if (Subtarget->isTargetDarwin() &&
763 TM.getRelocationModel() == Reloc::Static) {
764 if (GVar->getName() == "llvm.global_ctors")
765 O << ".reference .constructors_used\n";
766 else if (GVar->getName() == "llvm.global_dtors")
767 O << ".reference .destructors_used\n";
768 }
769 return;
770 }
771
772 std::string name = Mang->getValueName(GVar);
773 Constant *C = GVar->getInitializer();
774 const Type *Type = C->getType();
775 unsigned Size = TD->getABITypeSize(Type);
776 unsigned Align = TD->getPreferredAlignmentLog(GVar);
777
778 if (GVar->hasHiddenVisibility()) {
779 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000780 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000781 } else if (GVar->hasProtectedVisibility()) {
782 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000783 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000784 }
785
786 if (Subtarget->isTargetELF())
787 O << "\t.type\t" << name << ",@object\n";
788
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000789 SwitchToDataSection(SectionName.c_str());
790
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000791 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000792 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000793 if (GVar->hasExternalLinkage()) {
794 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000795 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000796 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000797 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000798 return;
799 }
800 }
801
802 if (!GVar->isThreadLocal() &&
Anton Korobeynikovc33a7442008-07-09 13:27:59 +0000803 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000804 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000805
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000806 if (TAI->getLCOMMDirective() != NULL) {
807 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000808 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000809 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000810 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000811 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000812 O << "\t.globl " << name << '\n'
813 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000814 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000815 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000816 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000817 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000818 EmitGlobalConstant(C);
819 return;
820 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000821 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000822
823 // Leopard and above support aligned common symbols.
824 if (Subtarget->getDarwinVers() >= 9)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000825 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000826 }
827 } else {
828 if (!Subtarget->isTargetCygMing()) {
829 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000830 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000831 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000832 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000834 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000836 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000837 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000838 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000839 return;
840 }
841 }
842
843 switch (GVar->getLinkage()) {
844 case GlobalValue::CommonLinkage:
845 case GlobalValue::LinkOnceLinkage:
846 case GlobalValue::WeakLinkage:
847 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000848 O << "\t.globl " << name << '\n'
849 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000851 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000852 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000853 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000854 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000855 }
856 break;
857 case GlobalValue::DLLExportLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000858 case GlobalValue::AppendingLinkage:
859 // FIXME: appending linkage variables should go into a section of
860 // their name or something. For now, just emit them as external.
861 case GlobalValue::ExternalLinkage:
862 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000863 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000864 // FALL THROUGH
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000865 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000866 break;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000867 default:
868 assert(0 && "Unknown linkage type!");
869 }
870
871 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000872 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000873 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000874 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000875 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000876 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000877
878 // If the initializer is a extern weak symbol, remember to emit the weak
879 // reference!
880 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
881 if (GV->hasExternalWeakLinkage())
882 ExtWeakSymbols.insert(GV);
883
884 EmitGlobalConstant(C);
885}
886
Evan Cheng77c8f762008-07-08 00:55:58 +0000887/// printGVStub - Print stub for a global value.
888///
889void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000890 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000891 O << ":\n\t.indirect_symbol ";
892 if (Prefix) O << Prefix;
893 O << GV << "\n\t.long\t0\n";
894}
895
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000896
897bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000898 // Print out module-level global variables here.
899 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000900 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000901 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000902
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000903 if (I->hasDLLExportLinkage())
904 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
905 }
906
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000907 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000908 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000909 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000910
911 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
912 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000913 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000914 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000915
916 if (!DLLExportedFns.empty()) {
917 SwitchToDataSection(".section .drectve");
918 }
919
920 for (StringSet<>::iterator i = DLLExportedFns.begin(),
921 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000922 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000923 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000924
925 if (Subtarget->isTargetDarwin()) {
926 SwitchToDataSection("");
927
928 // Output stubs for dynamically-linked functions
929 unsigned j = 1;
930 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
931 i != e; ++i, ++j) {
932 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
933 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +0000934 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000935 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +0000936 O << ":\n"
937 "\t.indirect_symbol " << p << "\n"
938 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000939 }
940
Dan Gohmand19a53b2008-06-30 22:03:41 +0000941 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000942
Evan Cheng77c8f762008-07-08 00:55:58 +0000943 // Print global value stubs.
944 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000945 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
946 // Add the (possibly multiple) personalities to the set of global values.
947 // Only referenced functions get into the Personalities list.
948 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000949 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +0000950 E = Personalities.end(); I != E; ++I) {
951 if (!*I)
952 continue;
953 if (!InStubSection) {
954 SwitchToDataSection(
955 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
956 InStubSection = true;
957 }
958 printGVStub((*I)->getNameStart(), "_");
959 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000960 }
961
962 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +0000963 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000964 SwitchToDataSection(
965 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
966 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +0000967 i != e; ++i)
968 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000969
970 // Emit final debug information.
971 DW.EndModule();
972
973 // Funny Darwin hack: This flag tells the linker that no global symbols
974 // contain code that falls through to other global symbols (e.g. the obvious
975 // implementation of multiple entry points). If this doesn't occur, the
976 // linker can safely perform dead code stripping. Since LLVM never
977 // generates code that does this, it is always safe to set.
978 O << "\t.subsections_via_symbols\n";
979 } else if (Subtarget->isTargetCygMing()) {
980 // Emit type information for external functions
981 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
982 i != e; ++i) {
983 O << "\t.def\t " << i->getKeyData()
984 << ";\t.scl\t" << COFF::C_EXT
985 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
986 << ";\t.endef\n";
987 }
988
989 // Emit final debug information.
990 DW.EndModule();
991 } else if (Subtarget->isTargetELF()) {
992 // Emit final debug information.
993 DW.EndModule();
994 }
995
996 return AsmPrinter::doFinalization(M);
997}
998
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000999// Include the auto-generated portion of the assembly writer.
1000#include "X86GenAsmWriter.inc"