blob: 0394b6c6cd8b6dadfcf4243d2d74208c32179384 [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 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 Korobeynikove87f52d2008-07-09 13:27:16 +0000160 SwitchToTextSection(TAI->SectionForGlobal(F).c_str(), F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000161
Evan Chenge22e62b2008-03-25 22:29:46 +0000162 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000163 switch (F->getLinkage()) {
164 default: assert(0 && "Unknown linkage type!");
165 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000166 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000167 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000168 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000169 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000170 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000171 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000172 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000173 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000174 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
178 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000182 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000184 }
185 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000186 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000187 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000188 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000189 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000190 } else if (F->hasProtectedVisibility()) {
191 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000192 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000193 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000194
195 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000196 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000197 else if (Subtarget->isTargetCygMing()) {
198 O << "\t.def\t " << CurrentFnName
199 << ";\t.scl\t" <<
200 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
201 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
202 << ";\t.endef\n";
203 }
204
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000205 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000206 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000207 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000208 (F->getLinkage() == Function::LinkOnceLinkage ||
209 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000210 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000211}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000212
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000213/// runOnMachineFunction - This uses the printMachineInstruction()
214/// method to print assembly for each instruction.
215///
216bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
217 const Function *F = MF.getFunction();
218 unsigned CC = F->getCallingConv();
219
Evan Cheng526be702008-07-09 06:36:53 +0000220 if (TAI->doesSupportDebugInformation()) {
221 // Let PassManager know we need debug information and relay
222 // the MachineModuleInfo address on to DwarfWriter.
223 MMI = &getAnalysis<MachineModuleInfo>();
224 DW.SetModuleInfo(MMI);
225 }
226
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000227 SetupMachineFunction(MF);
228 O << "\n\n";
229
230 // Populate function information map. Actually, We don't want to populate
231 // non-stdcall or non-fastcall functions' information right now.
232 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
233 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
234
235 // Print out constants referenced by the function
236 EmitConstantPool(MF.getConstantPool());
237
238 if (F->hasDLLExportLinkage())
239 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
240
241 // Print the 'header' of function
242 emitFunctionHeader(MF);
243
244 // Emit pre-function debug and/or EH information.
245 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
246 DW.BeginFunction(&MF);
247
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000248 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000249 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000250 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
251 I != E; ++I) {
252 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000253 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000254 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000255 O << '\n';
256 }
Bill Wendling824a7212008-01-26 09:03:52 +0000257 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
258 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000259 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000260 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000261 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000262 printMachineInstruction(II);
263 }
264 }
Evan Cheng67afece2006-08-28 22:14:16 +0000265
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000266 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
267 // If the function is empty, then we need to emit *something*. Otherwise,
268 // the function's label might be associated with something that it wasn't
269 // meant to be associated with. We emit a noop in this situation.
270 // We are assuming inline asms are code.
271 O << "\tnop\n";
272 }
273
Jim Laskey563321a2006-09-06 18:34:40 +0000274 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000275 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000276
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000277 // Emit post-function debug information.
278 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000279 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000280
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000281 // Print out jump tables referenced by the function.
282 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000283
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000284 // We didn't modify anything.
285 return false;
286}
287
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000288static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000289 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
290}
291
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000292static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
293 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
294 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
295}
296
297static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000298 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
299}
300
Chris Lattnera3b8c572006-02-06 23:41:19 +0000301void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000302 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000303 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000304 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000305 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000306 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000307 "Virtual registers should not make it this far!");
308 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000309 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000310 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000311 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
313 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000314 Reg = getX86SubSuperRegister(Reg, VT);
315 }
Evan Chengae270f62008-07-07 22:21:06 +0000316 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000317 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000318 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000319
Chris Lattner63b3d712006-05-04 17:21:20 +0000320 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000321 if (!Modifier ||
322 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000323 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000324 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000325 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000326 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000327 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000328 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000329 case MachineOperand::MO_JumpTableIndex: {
330 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
331 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000332 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000333 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000334
335 if (TM.getRelocationModel() == Reloc::PIC_) {
336 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000337 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
338 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000339 else if (Subtarget->isPICStyleGOT())
340 O << "@GOTOFF";
341 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000342
Evan Chengae19abc2007-01-18 22:27:12 +0000343 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000344 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000345 return;
346 }
Evan Chenga09bd812006-02-26 08:28:12 +0000347 case MachineOperand::MO_ConstantPoolIndex: {
348 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
349 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000350 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000351 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000352
353 if (TM.getRelocationModel() == Reloc::PIC_) {
354 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000355 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
356 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000357 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000358 O << "@GOTOFF";
359 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000360
Evan Chenga09bd812006-02-26 08:28:12 +0000361 int Offset = MO.getOffset();
362 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000363 O << '+' << Offset;
Evan Chenga09bd812006-02-26 08:28:12 +0000364 else if (Offset < 0)
365 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000366
Evan Chengae19abc2007-01-18 22:27:12 +0000367 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000368 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000369 return;
370 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000371 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000372 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000373 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000374 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000375
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000376 const GlobalValue *GV = MO.getGlobal();
377 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
378 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000379 // If GV is an alias then use the aliasee for determining
380 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000381 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
382 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
383 }
384
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000385 bool isThreadLocal = GVar && GVar->isThreadLocal();
386
Evan Cheng25ab6902006-09-08 06:48:29 +0000387 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000388 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000389
Dan Gohman2a3250c2007-04-26 21:07:05 +0000390 if (!isMemOp && !isCallOp)
391 O << '$';
392 else if (Name[0] == '$') {
393 // The name begins with a dollar-sign. In order to avoid having it look
394 // like an integer immediate to the assembler, enclose it in parens.
395 O << '(';
396 needCloseParen = true;
397 }
398
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000399 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000400 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000401 // non-lazily-resolved stubs
Reid Spencer5cbf9852007-01-30 20:08:39 +0000402 if (GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000403 GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000404 GV->hasLinkOnceLinkage() ||
405 GV->hasCommonLinkage()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000406 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000407 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000408 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000409 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000410 } else {
411 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000412 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000413 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000414 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000415 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000416 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000417 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000418 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000419
Chris Lattner35d86fe2006-07-26 21:12:04 +0000420 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000421 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000422 } else {
423 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000424 O << "__imp_";
425 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000426 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000427
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000428 if (isCallOp) {
429 if (shouldPrintPLT(TM, Subtarget)) {
430 // Assemble call via PLT for externally visible symbols
431 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
432 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000433 O << "@PLT";
434 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000435 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000436 // Save function name for later type emission
437 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000438 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000439 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000440
Evan Cheng6d7d3102006-12-01 07:38:23 +0000441 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000442 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000443
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000444 int Offset = MO.getOffset();
445 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000446 O << '+' << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000447 else if (Offset < 0)
448 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000449
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000450 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000451 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000452 O << "@TLSGD"; // general dynamic TLS model
453 else
454 if (GV->isDeclaration())
455 O << "@INDNTPOFF"; // initial exec TLS model
456 else
457 O << "@NTPOFF"; // local exec TLS model
458 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000459 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000460 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000461 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000462 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000463 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000464 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
465 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000466 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000467 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000468
469 if (needCloseParen) {
470 needCloseParen = false;
471 O << ')';
472 }
473
Evan Chengae19abc2007-01-18 22:27:12 +0000474 // Use rip when possible to reduce code size, except when
475 // index or base register are also part of the address. e.g.
476 // foo(%rip)(%rcx,%rax,4) is not legal
477 O << "(%rip)";
478 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000479 }
480
Dan Gohman2a3250c2007-04-26 21:07:05 +0000481 if (needCloseParen)
482 O << ')';
483
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000484 return;
485 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000486 case MachineOperand::MO_ExternalSymbol: {
487 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000488 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000489 std::string Name(TAI->getGlobalPrefix());
490 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000491 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000492 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000493 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000494 return;
495 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000496 if (!isCallOp)
497 O << '$';
498 else if (Name[0] == '$') {
499 // The name begins with a dollar-sign. In order to avoid having it look
500 // like an integer immediate to the assembler, enclose it in parens.
501 O << '(';
502 needCloseParen = true;
503 }
504
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000505 O << Name;
506
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000507 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000508 std::string GOTName(TAI->getGlobalPrefix());
509 GOTName+="_GLOBAL_OFFSET_TABLE_";
510 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000511 // HACK! Emit extra offset to PC during printing GOT offset to
512 // compensate for the size of popl instruction. The resulting code
513 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000514 // call .piclabel
515 // piclabel:
516 // popl %some_register
517 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000518 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000519 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000520
521 if (isCallOp)
522 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000523 }
524
Dan Gohman2a3250c2007-04-26 21:07:05 +0000525 if (needCloseParen)
526 O << ')';
527
Evan Chengae19abc2007-01-18 22:27:12 +0000528 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000529 O << "(%rip)";
530
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000531 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000532 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000533 default:
534 O << "<unknown operand type>"; return;
535 }
536}
537
Nate Begeman391c5d22005-11-30 18:54:35 +0000538void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000539 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000540 assert(value <= 7 && "Invalid ssecc argument!");
541 switch (value) {
542 case 0: O << "eq"; break;
543 case 1: O << "lt"; break;
544 case 2: O << "le"; break;
545 case 3: O << "unord"; break;
546 case 4: O << "neq"; break;
547 case 5: O << "nlt"; break;
548 case 6: O << "nle"; break;
549 case 7: O << "ord"; break;
550 }
551}
552
Evan Cheng25ab6902006-09-08 06:48:29 +0000553void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
554 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000555 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000556 MachineOperand BaseReg = MI->getOperand(Op);
557 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000558 const MachineOperand &DispSpec = MI->getOperand(Op+3);
559
Evan Cheng28b514392006-12-05 19:50:18 +0000560 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000561 if (DispSpec.isGlobalAddress() ||
562 DispSpec.isConstantPoolIndex() ||
563 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000564 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000565 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000566 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000567 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
568 O << DispVal;
569 }
570
571 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000572 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000573 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000574
Chris Lattner32c9a452007-01-14 00:13:07 +0000575 // There are cases where we can end up with ESP/RSP in the indexreg slot.
576 // If this happens, swap the base/index register to support assemblers that
577 // don't work when the index is *SP.
578 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
579 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
580 std::swap(BaseReg, IndexReg);
581 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000582 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000583
Dan Gohmand19a53b2008-06-30 22:03:41 +0000584 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000585 if (BaseReg.getReg())
586 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000587
588 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000589 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000590 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000591 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000592 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000593 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000594 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000595 }
596}
597
Anton Korobeynikov91364382008-06-28 11:08:09 +0000598void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000599 const MachineBasicBlock *MBB) const {
600 if (!TAI->getSetDirective())
601 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000602
603 // We don't need .set machinery if we have GOT-style relocations
604 if (Subtarget->isPICStyleGOT())
605 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000606
Evan Chengcc415862007-11-09 01:32:10 +0000607 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
608 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000609 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000610 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000611 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000612 << '_' << uid << '\n';
613 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000614 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000615}
616
Evan Cheng7ccced62006-02-18 00:15:05 +0000617void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000618 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000619 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000620}
621
Evan Cheng62f27002006-04-28 23:11:40 +0000622
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000623void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
624 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000625 unsigned uid) const
626{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000627 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
628 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
629
630 O << JTEntryDirective << ' ';
631
632 if (TM.getRelocationModel() == Reloc::PIC_) {
633 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
634 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
635 << '_' << uid << "_set_" << MBB->getNumber();
636 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000637 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000638 O << "@GOTOFF";
639 } else
640 assert(0 && "Don't know how to print MBB label for this PIC mode");
641 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000642 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000643}
644
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000645bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000646 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000647 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000648 switch (Mode) {
649 default: return true; // Unknown mode.
650 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000651 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000652 break;
653 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000654 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000655 break;
656 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000657 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000658 break;
659 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000660 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000661 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000662 case 'q': // Print DImode register
663 Reg = getX86SubSuperRegister(Reg, MVT::i64);
664 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000665 }
666
Evan Chengae270f62008-07-07 22:21:06 +0000667 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000668 return false;
669}
670
Evan Cheng3d48a902006-04-28 21:19:05 +0000671/// PrintAsmOperand - Print out an operand for an inline asm expression.
672///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000673bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000674 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000675 const char *ExtraCode) {
676 // Does this asm operand have a single letter operand modifier?
677 if (ExtraCode && ExtraCode[0]) {
678 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000679
Evan Cheng3d48a902006-04-28 21:19:05 +0000680 switch (ExtraCode[0]) {
681 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000682 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000683 printOperand(MI, OpNo, "mem");
684 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000685 case 'b': // Print QImode register
686 case 'h': // Print QImode high register
687 case 'w': // Print HImode register
688 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000689 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000690 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000691 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
692 printOperand(MI, OpNo);
693 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000694
Chris Lattner7cd5e072007-03-25 01:44:57 +0000695 case 'P': // Don't print @PLT, but do print as memory.
696 printOperand(MI, OpNo, "mem");
697 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000698 }
699 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000700
Evan Cheng3d48a902006-04-28 21:19:05 +0000701 printOperand(MI, OpNo);
702 return false;
703}
704
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000705bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000706 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000707 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000708 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000709 if (ExtraCode && ExtraCode[0]) {
710 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000711
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000712 switch (ExtraCode[0]) {
713 default: return true; // Unknown modifier.
714 case 'b': // Print QImode register
715 case 'h': // Print QImode high register
716 case 'w': // Print HImode register
717 case 'k': // Print SImode register
718 case 'q': // Print SImode register
719 // These only apply to registers, ignore on mem.
720 break;
721 }
722 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000723 printMemReference(MI, OpNo);
724 return false;
725}
726
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000727/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000728/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000729///
730void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
731 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000732
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000733 // Call the autogenerated instruction printer routines.
734 printInstruction(MI);
735}
736
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000737/// doInitialization
738bool X86ATTAsmPrinter::doInitialization(Module &M) {
739 if (TAI->doesSupportDebugInformation()) {
740 // Emit initial debug information.
741 DW.BeginModule(&M);
742 }
743
Evan Cheng526be702008-07-09 06:36:53 +0000744 bool Result = AsmPrinter::doInitialization(M);
745
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000746 // Darwin wants symbols to be quoted if they have complex names.
747 if (Subtarget->isTargetDarwin())
748 Mang->setUseQuotes(true);
749
750 return Result;
751}
752
753
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000754void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000755 const TargetData *TD = TM.getTargetData();
756
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000757 if (!GVar->hasInitializer())
758 return; // External global require no code
759
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000760 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikov0e48a0c2008-07-09 13:24:38 +0000761
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000762 // Check to see if this is a special global used by LLVM, if so, emit it.
763 if (EmitSpecialLLVMGlobal(GVar)) {
764 if (Subtarget->isTargetDarwin() &&
765 TM.getRelocationModel() == Reloc::Static) {
766 if (GVar->getName() == "llvm.global_ctors")
767 O << ".reference .constructors_used\n";
768 else if (GVar->getName() == "llvm.global_dtors")
769 O << ".reference .destructors_used\n";
770 }
771 return;
772 }
773
774 std::string name = Mang->getValueName(GVar);
775 Constant *C = GVar->getInitializer();
776 const Type *Type = C->getType();
777 unsigned Size = TD->getABITypeSize(Type);
778 unsigned Align = TD->getPreferredAlignmentLog(GVar);
779
780 if (GVar->hasHiddenVisibility()) {
781 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000782 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000783 } else if (GVar->hasProtectedVisibility()) {
784 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000785 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000786 }
787
788 if (Subtarget->isTargetELF())
789 O << "\t.type\t" << name << ",@object\n";
790
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000791 SwitchToDataSection(SectionName.c_str());
792
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000793 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000794 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000795 if (GVar->hasExternalLinkage()) {
796 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000797 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000798 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000799 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000800 return;
801 }
802 }
803
804 if (!GVar->isThreadLocal() &&
805 (GVar->hasInternalLinkage() || GVar->hasWeakLinkage() ||
806 GVar->hasLinkOnceLinkage() || GVar->hasCommonLinkage())) {
807 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000808
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000809 if (TAI->getLCOMMDirective() != NULL) {
810 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000811 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000812 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000813 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000814 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000815 O << "\t.globl " << name << '\n'
816 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000817 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000818 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000819 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000820 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000821 EmitGlobalConstant(C);
822 return;
823 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000824 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000825
826 // Leopard and above support aligned common symbols.
827 if (Subtarget->getDarwinVers() >= 9)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000828 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000829 }
830 } else {
831 if (!Subtarget->isTargetCygMing()) {
832 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000833 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000834 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000835 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000836 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000837 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000838 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000839 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000840 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000841 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000842 return;
843 }
844 }
845
846 switch (GVar->getLinkage()) {
847 case GlobalValue::CommonLinkage:
848 case GlobalValue::LinkOnceLinkage:
849 case GlobalValue::WeakLinkage:
850 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << "\t.globl " << name << '\n'
852 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000853 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000854 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000855 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000857 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000858 }
859 break;
860 case GlobalValue::DLLExportLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000861 case GlobalValue::AppendingLinkage:
862 // FIXME: appending linkage variables should go into a section of
863 // their name or something. For now, just emit them as external.
864 case GlobalValue::ExternalLinkage:
865 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000866 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000867 // FALL THROUGH
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000868 case GlobalValue::InternalLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000869 break;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000870 default:
871 assert(0 && "Unknown linkage type!");
872 }
873
874 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000875 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000876 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000877 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000878 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000879 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000880
881 // If the initializer is a extern weak symbol, remember to emit the weak
882 // reference!
883 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
884 if (GV->hasExternalWeakLinkage())
885 ExtWeakSymbols.insert(GV);
886
887 EmitGlobalConstant(C);
888}
889
Evan Cheng77c8f762008-07-08 00:55:58 +0000890/// printGVStub - Print stub for a global value.
891///
892void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000893 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000894 O << ":\n\t.indirect_symbol ";
895 if (Prefix) O << Prefix;
896 O << GV << "\n\t.long\t0\n";
897}
898
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000899
900bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000901 // Print out module-level global variables here.
902 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000903 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000904 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000905
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000906 if (I->hasDLLExportLinkage())
907 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
908 }
909
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000910 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000911 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000912 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000913
914 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
915 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000916 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000917 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000918
919 if (!DLLExportedFns.empty()) {
920 SwitchToDataSection(".section .drectve");
921 }
922
923 for (StringSet<>::iterator i = DLLExportedFns.begin(),
924 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000925 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000926 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000927
928 if (Subtarget->isTargetDarwin()) {
929 SwitchToDataSection("");
930
931 // Output stubs for dynamically-linked functions
932 unsigned j = 1;
933 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
934 i != e; ++i, ++j) {
935 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
936 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +0000937 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000938 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +0000939 O << ":\n"
940 "\t.indirect_symbol " << p << "\n"
941 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000942 }
943
Dan Gohmand19a53b2008-06-30 22:03:41 +0000944 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000945
Evan Cheng77c8f762008-07-08 00:55:58 +0000946 // Print global value stubs.
947 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000948 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
949 // Add the (possibly multiple) personalities to the set of global values.
950 // Only referenced functions get into the Personalities list.
951 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000952 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +0000953 E = Personalities.end(); I != E; ++I) {
954 if (!*I)
955 continue;
956 if (!InStubSection) {
957 SwitchToDataSection(
958 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
959 InStubSection = true;
960 }
961 printGVStub((*I)->getNameStart(), "_");
962 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000963 }
964
965 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +0000966 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000967 SwitchToDataSection(
968 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
969 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +0000970 i != e; ++i)
971 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000972
973 // Emit final debug information.
974 DW.EndModule();
975
976 // Funny Darwin hack: This flag tells the linker that no global symbols
977 // contain code that falls through to other global symbols (e.g. the obvious
978 // implementation of multiple entry points). If this doesn't occur, the
979 // linker can safely perform dead code stripping. Since LLVM never
980 // generates code that does this, it is always safe to set.
981 O << "\t.subsections_via_symbols\n";
982 } else if (Subtarget->isTargetCygMing()) {
983 // Emit type information for external functions
984 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
985 i != e; ++i) {
986 O << "\t.def\t " << i->getKeyData()
987 << ";\t.scl\t" << COFF::C_EXT
988 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
989 << ";\t.endef\n";
990 }
991
992 // Emit final debug information.
993 DW.EndModule();
994 } else if (Subtarget->isTargetELF()) {
995 // Emit final debug information.
996 DW.EndModule();
997 }
998
999 return AsmPrinter::doFinalization(M);
1000}
1001
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001002// Include the auto-generated portion of the assembly writer.
1003#include "X86GenAsmWriter.inc"