blob: 6e83cbc5915a626d0d4790c5814d044ea60fd2a9 [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
18#include "X86.h"
Anton Korobeynikovd05ca652007-01-16 16:41:57 +000019#include "X86COFF.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000020#include "X86MachineFunctionInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000021#include "X86TargetMachine.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000022#include "X86TargetAsmInfo.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/CallingConv.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/Module.h"
Anton Korobeynikov75b68822008-06-28 11:08:27 +000026#include "llvm/Type.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034
Chris Lattner95b2c7d2006-12-19 22:59:26 +000035STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
Evan Cheng0475ab52008-01-05 00:41:47 +000037static std::string getPICLabelString(unsigned FnNum,
38 const TargetAsmInfo *TAI,
39 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000040 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000041 if (Subtarget->isTargetDarwin())
Evan Cheng347d39f2007-10-14 05:57:21 +000042 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Evan Cheng071b9d52007-01-18 01:49:58 +000043 else if (Subtarget->isTargetELF())
Dan Gohmand19a53b2008-06-30 22:03:41 +000044 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Evan Cheng071b9d52007-01-18 01:49:58 +000045 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000046 assert(0 && "Don't know how to print PIC label!\n");
47
48 return label;
49}
50
Anton Korobeynikov75b68822008-06-28 11:08:27 +000051static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
52 const TargetData *TD) {
53 X86MachineFunctionInfo Info;
54 uint64_t Size = 0;
55
56 switch (F->getCallingConv()) {
57 case CallingConv::X86_StdCall:
58 Info.setDecorationStyle(StdCall);
59 break;
60 case CallingConv::X86_FastCall:
61 Info.setDecorationStyle(FastCall);
62 break;
63 default:
64 return Info;
65 }
66
67 unsigned argNum = 1;
68 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
69 AI != AE; ++AI, ++argNum) {
70 const Type* Ty = AI->getType();
71
72 // 'Dereference' type in case of byval parameter attribute
73 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
74 Ty = cast<PointerType>(Ty)->getElementType();
75
76 // Size should be aligned to DWORD boundary
77 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
78 }
79
80 // We're not supporting tooooo huge arguments :)
81 Info.setBytesToPopOnReturn((unsigned int)Size);
82 return Info;
83}
84
85/// PrintUnmangledNameSafely - Print out the printable characters in the name.
86/// Don't print things like \n or \0.
87static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
88 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
89 Name != E; ++Name)
90 if (isprint(*Name))
91 OS << *Name;
92}
93
94/// decorateName - Query FunctionInfoMap and use this information for various
95/// name decoration.
96void X86ATTAsmPrinter::decorateName(std::string &Name,
97 const GlobalValue *GV) {
98 const Function *F = dyn_cast<Function>(GV);
99 if (!F) return;
100
101 // We don't want to decorate non-stdcall or non-fastcall functions right now
102 unsigned CC = F->getCallingConv();
103 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
104 return;
105
106 // Decorate names only when we're targeting Cygwin/Mingw32 targets
107 if (!Subtarget->isTargetCygMing())
108 return;
109
110 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
111
112 const X86MachineFunctionInfo *Info;
113 if (info_item == FunctionInfoMap.end()) {
114 // Calculate apropriate function info and populate map
115 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
116 Info = &FunctionInfoMap[F];
117 } else {
118 Info = &info_item->second;
119 }
120
121 const FunctionType *FT = F->getFunctionType();
122 switch (Info->getDecorationStyle()) {
123 case None:
124 break;
125 case StdCall:
126 // "Pure" variadic functions do not receive @0 suffix.
127 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
128 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
129 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
130 break;
131 case FastCall:
132 // "Pure" variadic functions do not receive @0 suffix.
133 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
134 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
135 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
136
137 if (Name[0] == '_') {
138 Name[0] = '@';
139 } else {
140 Name = '@' + Name;
141 }
142 break;
143 default:
144 assert(0 && "Unsupported DecorationStyle");
145 }
146}
147
Chris Lattnerafbfded2006-10-05 02:43:52 +0000148/// getSectionForFunction - Return the section that we should emit the
149/// specified function body into.
150std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
151 switch (F.getLinkage()) {
152 default: assert(0 && "Unknown linkage type!");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000153 case Function::InternalLinkage:
Chris Lattnerafbfded2006-10-05 02:43:52 +0000154 case Function::DLLExportLinkage:
155 case Function::ExternalLinkage:
156 return TAI->getTextSection();
157 case Function::WeakLinkage:
158 case Function::LinkOnceLinkage:
159 if (Subtarget->isTargetDarwin()) {
160 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000161 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmanaf67ea72007-06-14 15:00:27 +0000162 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
Chris Lattnerafbfded2006-10-05 02:43:52 +0000163 } else {
164 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
Dan Gohmanaf67ea72007-06-14 15:00:27 +0000165 ",\"ax\",@progbits";
Chris Lattnerafbfded2006-10-05 02:43:52 +0000166 }
167 }
168}
169
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000170void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000171 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000172
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000173 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000174
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +0000175 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000176
Evan Chenge22e62b2008-03-25 22:29:46 +0000177 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000178 switch (F->getLinkage()) {
179 default: assert(0 && "Unknown linkage type!");
180 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000181 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000182 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000183 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000184 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000185 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000186 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000187 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000188 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000189 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000190 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000191 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000192 O << "\t.globl\t" << CurrentFnName << '\n';
193 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000194 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000195 O << "\t.globl\t" << CurrentFnName << "\n"
196 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000197 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000198 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000199 }
200 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000201 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000202 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000203 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000204 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000205 } else if (F->hasProtectedVisibility()) {
206 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000207 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000208 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000209
210 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000211 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000212 else if (Subtarget->isTargetCygMing()) {
213 O << "\t.def\t " << CurrentFnName
214 << ";\t.scl\t" <<
215 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
216 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
217 << ";\t.endef\n";
218 }
219
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000220 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000221 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000222 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000223 (F->getLinkage() == Function::LinkOnceLinkage ||
224 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000225 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000226}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000227
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000228/// runOnMachineFunction - This uses the printMachineInstruction()
229/// method to print assembly for each instruction.
230///
231bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
232 const Function *F = MF.getFunction();
233 unsigned CC = F->getCallingConv();
234
235 if (TAI->doesSupportDebugInformation()) {
236 // Let PassManager know we need debug information and relay
237 // the MachineModuleInfo address on to DwarfWriter.
238 MMI = &getAnalysis<MachineModuleInfo>();
239 DW.SetModuleInfo(MMI);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000240 }
241
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000242 SetupMachineFunction(MF);
243 O << "\n\n";
244
245 // Populate function information map. Actually, We don't want to populate
246 // non-stdcall or non-fastcall functions' information right now.
247 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
248 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
249
250 // Print out constants referenced by the function
251 EmitConstantPool(MF.getConstantPool());
252
253 if (F->hasDLLExportLinkage())
254 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
255
256 // Print the 'header' of function
257 emitFunctionHeader(MF);
258
259 // Emit pre-function debug and/or EH information.
260 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
261 DW.BeginFunction(&MF);
262
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000263 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000264 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000265 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
266 I != E; ++I) {
267 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000268 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000269 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000270 O << '\n';
271 }
Bill Wendling824a7212008-01-26 09:03:52 +0000272 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
273 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000275 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000276 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000277 printMachineInstruction(II);
278 }
279 }
Evan Cheng67afece2006-08-28 22:14:16 +0000280
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000281 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
282 // If the function is empty, then we need to emit *something*. Otherwise,
283 // the function's label might be associated with something that it wasn't
284 // meant to be associated with. We emit a noop in this situation.
285 // We are assuming inline asms are code.
286 O << "\tnop\n";
287 }
288
Jim Laskey563321a2006-09-06 18:34:40 +0000289 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000290 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000291
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000292 // Emit post-function debug information.
293 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000294 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000295
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000296 // Print out jump tables referenced by the function.
297 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000298
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000299 // We didn't modify anything.
300 return false;
301}
302
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000303static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000304 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
305}
306
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000307static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
308 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
309 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
310}
311
312static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000313 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
314}
315
Chris Lattnera3b8c572006-02-06 23:41:19 +0000316void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000317 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000318 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000319 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000320 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000321 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000322 "Virtual registers should not make it this far!");
323 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000324 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000325 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000326 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
328 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000329 Reg = getX86SubSuperRegister(Reg, VT);
330 }
Evan Chengae270f62008-07-07 22:21:06 +0000331 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000332 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000333 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000334
Chris Lattner63b3d712006-05-04 17:21:20 +0000335 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000336 if (!Modifier ||
337 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000338 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000339 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000340 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000341 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000342 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000343 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000344 case MachineOperand::MO_JumpTableIndex: {
345 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
346 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000347 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000348 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000349
350 if (TM.getRelocationModel() == Reloc::PIC_) {
351 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000352 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
353 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000354 else if (Subtarget->isPICStyleGOT())
355 O << "@GOTOFF";
356 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000357
Evan Chengae19abc2007-01-18 22:27:12 +0000358 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000359 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000360 return;
361 }
Evan Chenga09bd812006-02-26 08:28:12 +0000362 case MachineOperand::MO_ConstantPoolIndex: {
363 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
364 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000365 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000366 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000367
368 if (TM.getRelocationModel() == Reloc::PIC_) {
369 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000370 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
371 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000372 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000373 O << "@GOTOFF";
374 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000375
Evan Chenga09bd812006-02-26 08:28:12 +0000376 int Offset = MO.getOffset();
377 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000378 O << '+' << Offset;
Evan Chenga09bd812006-02-26 08:28:12 +0000379 else if (Offset < 0)
380 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000381
Evan Chengae19abc2007-01-18 22:27:12 +0000382 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000383 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000384 return;
385 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000386 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000387 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000388 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000389 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000390
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000391 const GlobalValue *GV = MO.getGlobal();
392 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
393 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000394 // If GV is an alias then use the aliasee for determining
395 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000396 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
397 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
398 }
399
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000400 bool isThreadLocal = GVar && GVar->isThreadLocal();
401
Evan Cheng25ab6902006-09-08 06:48:29 +0000402 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000403 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000404
Dan Gohman2a3250c2007-04-26 21:07:05 +0000405 if (!isMemOp && !isCallOp)
406 O << '$';
407 else if (Name[0] == '$') {
408 // The name begins with a dollar-sign. In order to avoid having it look
409 // like an integer immediate to the assembler, enclose it in parens.
410 O << '(';
411 needCloseParen = true;
412 }
413
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000414 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000415 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000416 // non-lazily-resolved stubs
Reid Spencer5cbf9852007-01-30 20:08:39 +0000417 if (GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000418 GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000419 GV->hasLinkOnceLinkage() ||
420 GV->hasCommonLinkage()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000421 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000422 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000423 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000424 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000425 } else {
426 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000427 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000428 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000429 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000430 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000431 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000432 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000433 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000434
Chris Lattner35d86fe2006-07-26 21:12:04 +0000435 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000436 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000437 } else {
438 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000439 O << "__imp_";
440 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000441 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000442
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000443 if (isCallOp) {
444 if (shouldPrintPLT(TM, Subtarget)) {
445 // Assemble call via PLT for externally visible symbols
446 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
447 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000448 O << "@PLT";
449 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000450 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000451 // Save function name for later type emission
452 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000453 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000454 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000455
Evan Cheng6d7d3102006-12-01 07:38:23 +0000456 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000457 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000458
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000459 int Offset = MO.getOffset();
460 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000461 O << '+' << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000462 else if (Offset < 0)
463 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000464
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000465 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000466 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000467 O << "@TLSGD"; // general dynamic TLS model
468 else
469 if (GV->isDeclaration())
470 O << "@INDNTPOFF"; // initial exec TLS model
471 else
472 O << "@NTPOFF"; // local exec TLS model
473 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000474 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000475 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000476 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000477 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000478 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000479 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
480 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000481 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000482 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000483
484 if (needCloseParen) {
485 needCloseParen = false;
486 O << ')';
487 }
488
Evan Chengae19abc2007-01-18 22:27:12 +0000489 // Use rip when possible to reduce code size, except when
490 // index or base register are also part of the address. e.g.
491 // foo(%rip)(%rcx,%rax,4) is not legal
492 O << "(%rip)";
493 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000494 }
495
Dan Gohman2a3250c2007-04-26 21:07:05 +0000496 if (needCloseParen)
497 O << ')';
498
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000499 return;
500 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000501 case MachineOperand::MO_ExternalSymbol: {
502 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000503 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000504 std::string Name(TAI->getGlobalPrefix());
505 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000506 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000507 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000508 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000509 return;
510 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000511 if (!isCallOp)
512 O << '$';
513 else if (Name[0] == '$') {
514 // The name begins with a dollar-sign. In order to avoid having it look
515 // like an integer immediate to the assembler, enclose it in parens.
516 O << '(';
517 needCloseParen = true;
518 }
519
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000520 O << Name;
521
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000522 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000523 std::string GOTName(TAI->getGlobalPrefix());
524 GOTName+="_GLOBAL_OFFSET_TABLE_";
525 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000526 // HACK! Emit extra offset to PC during printing GOT offset to
527 // compensate for the size of popl instruction. The resulting code
528 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000529 // call .piclabel
530 // piclabel:
531 // popl %some_register
532 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000533 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000534 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000535
536 if (isCallOp)
537 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000538 }
539
Dan Gohman2a3250c2007-04-26 21:07:05 +0000540 if (needCloseParen)
541 O << ')';
542
Evan Chengae19abc2007-01-18 22:27:12 +0000543 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000544 O << "(%rip)";
545
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000546 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000547 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000548 default:
549 O << "<unknown operand type>"; return;
550 }
551}
552
Nate Begeman391c5d22005-11-30 18:54:35 +0000553void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000554 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000555 assert(value <= 7 && "Invalid ssecc argument!");
556 switch (value) {
557 case 0: O << "eq"; break;
558 case 1: O << "lt"; break;
559 case 2: O << "le"; break;
560 case 3: O << "unord"; break;
561 case 4: O << "neq"; break;
562 case 5: O << "nlt"; break;
563 case 6: O << "nle"; break;
564 case 7: O << "ord"; break;
565 }
566}
567
Evan Cheng25ab6902006-09-08 06:48:29 +0000568void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
569 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000570 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000571 MachineOperand BaseReg = MI->getOperand(Op);
572 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000573 const MachineOperand &DispSpec = MI->getOperand(Op+3);
574
Evan Cheng28b514392006-12-05 19:50:18 +0000575 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000576 if (DispSpec.isGlobalAddress() ||
577 DispSpec.isConstantPoolIndex() ||
578 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000579 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000580 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000581 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000582 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
583 O << DispVal;
584 }
585
586 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000587 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000588 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000589
Chris Lattner32c9a452007-01-14 00:13:07 +0000590 // There are cases where we can end up with ESP/RSP in the indexreg slot.
591 // If this happens, swap the base/index register to support assemblers that
592 // don't work when the index is *SP.
593 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
594 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
595 std::swap(BaseReg, IndexReg);
596 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000597 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000598
Dan Gohmand19a53b2008-06-30 22:03:41 +0000599 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000600 if (BaseReg.getReg())
601 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000602
603 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000604 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000605 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000606 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000607 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000608 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000609 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000610 }
611}
612
Anton Korobeynikov91364382008-06-28 11:08:09 +0000613void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000614 const MachineBasicBlock *MBB) const {
615 if (!TAI->getSetDirective())
616 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000617
618 // We don't need .set machinery if we have GOT-style relocations
619 if (Subtarget->isPICStyleGOT())
620 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000621
Evan Chengcc415862007-11-09 01:32:10 +0000622 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
623 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000624 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000625 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000626 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000627 << '_' << uid << '\n';
628 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000629 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000630}
631
Evan Cheng7ccced62006-02-18 00:15:05 +0000632void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000633 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000634 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000635}
636
Evan Cheng62f27002006-04-28 23:11:40 +0000637
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000638void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
639 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000640 unsigned uid) const
641{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000642 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
643 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
644
645 O << JTEntryDirective << ' ';
646
647 if (TM.getRelocationModel() == Reloc::PIC_) {
648 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
649 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
650 << '_' << uid << "_set_" << MBB->getNumber();
651 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000652 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000653 O << "@GOTOFF";
654 } else
655 assert(0 && "Don't know how to print MBB label for this PIC mode");
656 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000657 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000658}
659
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000660bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000661 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000662 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000663 switch (Mode) {
664 default: return true; // Unknown mode.
665 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000666 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000667 break;
668 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000669 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000670 break;
671 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000672 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000673 break;
674 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000675 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000676 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000677 case 'q': // Print DImode register
678 Reg = getX86SubSuperRegister(Reg, MVT::i64);
679 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000680 }
681
Evan Chengae270f62008-07-07 22:21:06 +0000682 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000683 return false;
684}
685
Evan Cheng3d48a902006-04-28 21:19:05 +0000686/// PrintAsmOperand - Print out an operand for an inline asm expression.
687///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000688bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000689 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000690 const char *ExtraCode) {
691 // Does this asm operand have a single letter operand modifier?
692 if (ExtraCode && ExtraCode[0]) {
693 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000694
Evan Cheng3d48a902006-04-28 21:19:05 +0000695 switch (ExtraCode[0]) {
696 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000697 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000698 printOperand(MI, OpNo, "mem");
699 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000700 case 'b': // Print QImode register
701 case 'h': // Print QImode high register
702 case 'w': // Print HImode register
703 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000704 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000705 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000706 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
707 printOperand(MI, OpNo);
708 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000709
Chris Lattner7cd5e072007-03-25 01:44:57 +0000710 case 'P': // Don't print @PLT, but do print as memory.
711 printOperand(MI, OpNo, "mem");
712 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000713 }
714 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000715
Evan Cheng3d48a902006-04-28 21:19:05 +0000716 printOperand(MI, OpNo);
717 return false;
718}
719
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000720bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000721 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000722 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000723 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000724 if (ExtraCode && ExtraCode[0]) {
725 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000726
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000727 switch (ExtraCode[0]) {
728 default: return true; // Unknown modifier.
729 case 'b': // Print QImode register
730 case 'h': // Print QImode high register
731 case 'w': // Print HImode register
732 case 'k': // Print SImode register
733 case 'q': // Print SImode register
734 // These only apply to registers, ignore on mem.
735 break;
736 }
737 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000738 printMemReference(MI, OpNo);
739 return false;
740}
741
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000742/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000743/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000744///
745void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
746 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000747
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000748 // Call the autogenerated instruction printer routines.
749 printInstruction(MI);
750}
751
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000752/// doInitialization
753bool X86ATTAsmPrinter::doInitialization(Module &M) {
754 if (TAI->doesSupportDebugInformation()) {
755 // Emit initial debug information.
756 DW.BeginModule(&M);
757 }
758
759 bool Result = AsmPrinter::doInitialization(M);
760
761 // Darwin wants symbols to be quoted if they have complex names.
762 if (Subtarget->isTargetDarwin())
763 Mang->setUseQuotes(true);
764
765 return Result;
766}
767
768
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000769void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000770 const TargetData *TD = TM.getTargetData();
771
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000772 if (!GVar->hasInitializer())
773 return; // External global require no code
774
775 // Check to see if this is a special global used by LLVM, if so, emit it.
776 if (EmitSpecialLLVMGlobal(GVar)) {
777 if (Subtarget->isTargetDarwin() &&
778 TM.getRelocationModel() == Reloc::Static) {
779 if (GVar->getName() == "llvm.global_ctors")
780 O << ".reference .constructors_used\n";
781 else if (GVar->getName() == "llvm.global_dtors")
782 O << ".reference .destructors_used\n";
783 }
784 return;
785 }
786
787 std::string name = Mang->getValueName(GVar);
788 Constant *C = GVar->getInitializer();
789 const Type *Type = C->getType();
790 unsigned Size = TD->getABITypeSize(Type);
791 unsigned Align = TD->getPreferredAlignmentLog(GVar);
792
793 if (GVar->hasHiddenVisibility()) {
794 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000795 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000796 } else if (GVar->hasProtectedVisibility()) {
797 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000798 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000799 }
800
801 if (Subtarget->isTargetELF())
802 O << "\t.type\t" << name << ",@object\n";
803
804 if (C->isNullValue() && !GVar->hasSection()) {
805 if (GVar->hasExternalLinkage()) {
806 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000807 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000808 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000809 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000810 return;
811 }
812 }
813
814 if (!GVar->isThreadLocal() &&
815 (GVar->hasInternalLinkage() || GVar->hasWeakLinkage() ||
816 GVar->hasLinkOnceLinkage() || GVar->hasCommonLinkage())) {
817 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
818 if (!NoZerosInBSS && TAI->getBSSSection())
819 SwitchToDataSection(TAI->getBSSSection(), GVar);
820 else
821 SwitchToDataSection(TAI->getDataSection(), GVar);
822 if (TAI->getLCOMMDirective() != NULL) {
823 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000824 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000825 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000826 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000827 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000828 O << "\t.globl " << name << '\n'
829 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000830 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
831 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000832 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000833 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000834 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 EmitGlobalConstant(C);
836 return;
837 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000838 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000839
840 // Leopard and above support aligned common symbols.
841 if (Subtarget->getDarwinVers() >= 9)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000842 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000843 }
844 } else {
845 if (!Subtarget->isTargetCygMing()) {
846 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000847 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000849 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000850 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000851 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000852 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000853 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000854 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000855 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000856 return;
857 }
858 }
859
860 switch (GVar->getLinkage()) {
861 case GlobalValue::CommonLinkage:
862 case GlobalValue::LinkOnceLinkage:
863 case GlobalValue::WeakLinkage:
864 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000865 O << "\t.globl " << name << '\n'
866 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000867 if (!GVar->isConstant())
868 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
869 else {
870 const ArrayType *AT = dyn_cast<ArrayType>(Type);
871 if (AT && AT->getElementType()==Type::Int8Ty)
872 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", GVar);
873 else
874 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", GVar);
875 }
876 } else if (Subtarget->isTargetCygMing()) {
877 std::string SectionName(".section\t.data$linkonce." +
878 name +
879 ",\"aw\"");
880 SwitchToDataSection(SectionName.c_str(), GVar);
881 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000882 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000883 } else {
884 std::string SectionName("\t.section\t.llvm.linkonce.d." +
885 name +
886 ",\"aw\",@progbits");
887 SwitchToDataSection(SectionName.c_str(), GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000888 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000889 }
890 break;
891 case GlobalValue::DLLExportLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000892 case GlobalValue::AppendingLinkage:
893 // FIXME: appending linkage variables should go into a section of
894 // their name or something. For now, just emit them as external.
895 case GlobalValue::ExternalLinkage:
896 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000897 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000898 // FALL THROUGH
899 case GlobalValue::InternalLinkage: {
900 if (GVar->isConstant()) {
901 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
902 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
903 SwitchToDataSection(TAI->getCStringSection(), GVar);
904 break;
905 }
906 }
907 // FIXME: special handling for ".ctors" & ".dtors" sections
908 if (GVar->hasSection() &&
909 (GVar->getSection() == ".ctors" || GVar->getSection() == ".dtors")) {
910 std::string SectionName = ".section " + GVar->getSection();
911
912 if (Subtarget->isTargetCygMing()) {
913 SectionName += ",\"aw\"";
914 } else {
915 assert(!Subtarget->isTargetDarwin());
916 SectionName += ",\"aw\",@progbits";
917 }
918 SwitchToDataSection(SectionName.c_str());
919 } else if (GVar->hasSection() && Subtarget->isTargetDarwin()) {
920 // Honor all section names on Darwin; ObjC uses this
921 std::string SectionName = ".section " + GVar->getSection();
922 SwitchToDataSection(SectionName.c_str());
923 } else {
924 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
925 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSBSSSection() :
926 TAI->getBSSSection(), GVar);
927 else if (!GVar->isConstant())
928 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSDataSection() :
929 TAI->getDataSection(), GVar);
930 else if (GVar->isThreadLocal())
931 SwitchToDataSection(TAI->getTLSDataSection());
932 else {
933 // Read-only data.
934 bool HasReloc = C->ContainsRelocations();
935 if (HasReloc &&
936 Subtarget->isTargetDarwin() &&
937 TM.getRelocationModel() != Reloc::Static)
938 SwitchToDataSection("\t.const_data\n");
939 else if (!HasReloc && Size == 4 &&
940 TAI->getFourByteConstantSection())
941 SwitchToDataSection(TAI->getFourByteConstantSection(), GVar);
942 else if (!HasReloc && Size == 8 &&
943 TAI->getEightByteConstantSection())
944 SwitchToDataSection(TAI->getEightByteConstantSection(), GVar);
945 else if (!HasReloc && Size == 16 &&
946 TAI->getSixteenByteConstantSection())
947 SwitchToDataSection(TAI->getSixteenByteConstantSection(), GVar);
948 else if (TAI->getReadOnlySection())
949 SwitchToDataSection(TAI->getReadOnlySection(), GVar);
950 else
951 SwitchToDataSection(TAI->getDataSection(), GVar);
952 }
953 }
954
955 break;
956 }
957 default:
958 assert(0 && "Unknown linkage type!");
959 }
960
961 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000962 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000963 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000964 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000965 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000966 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000967
968 // If the initializer is a extern weak symbol, remember to emit the weak
969 // reference!
970 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
971 if (GV->hasExternalWeakLinkage())
972 ExtWeakSymbols.insert(GV);
973
974 EmitGlobalConstant(C);
975}
976
977
978bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000979 // Print out module-level global variables here.
980 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000981 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000982 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000983
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000984 if (I->hasDLLExportLinkage())
985 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
986 }
987
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000988 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000989 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000990 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000991
992 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
993 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000994 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000995 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000996
997 if (!DLLExportedFns.empty()) {
998 SwitchToDataSection(".section .drectve");
999 }
1000
1001 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1002 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001003 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001004 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001005
1006 if (Subtarget->isTargetDarwin()) {
1007 SwitchToDataSection("");
1008
1009 // Output stubs for dynamically-linked functions
1010 unsigned j = 1;
1011 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1012 i != e; ++i, ++j) {
1013 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1014 "self_modifying_code+pure_instructions,5", 0);
1015 std::string p = i->getKeyData();
1016 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +00001017 O << ":\n"
1018 "\t.indirect_symbol " << p << "\n"
1019 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001020 }
1021
Dan Gohmand19a53b2008-06-30 22:03:41 +00001022 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001023
1024 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1025 // Add the (possibly multiple) personalities to the set of global values.
1026 // Only referenced functions get into the Personalities list.
1027 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1028
1029 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1030 E = Personalities.end(); I != E; ++I)
Dan Gohmand19a53b2008-06-30 22:03:41 +00001031 if (*I) GVStubs.insert('_' + (*I)->getName());
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001032 }
1033
1034 // Output stubs for external and common global variables.
1035 if (!GVStubs.empty())
1036 SwitchToDataSection(
1037 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1038 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1039 i != e; ++i) {
1040 std::string p = i->getKeyData();
1041 printSuffixedName(p, "$non_lazy_ptr");
Dan Gohmand19a53b2008-06-30 22:03:41 +00001042 O << ":\n"
1043 "\t.indirect_symbol " << p << "\n"
1044 "\t.long\t0\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001045 }
1046
1047 // Emit final debug information.
1048 DW.EndModule();
1049
1050 // Funny Darwin hack: This flag tells the linker that no global symbols
1051 // contain code that falls through to other global symbols (e.g. the obvious
1052 // implementation of multiple entry points). If this doesn't occur, the
1053 // linker can safely perform dead code stripping. Since LLVM never
1054 // generates code that does this, it is always safe to set.
1055 O << "\t.subsections_via_symbols\n";
1056 } else if (Subtarget->isTargetCygMing()) {
1057 // Emit type information for external functions
1058 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1059 i != e; ++i) {
1060 O << "\t.def\t " << i->getKeyData()
1061 << ";\t.scl\t" << COFF::C_EXT
1062 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1063 << ";\t.endef\n";
1064 }
1065
1066 // Emit final debug information.
1067 DW.EndModule();
1068 } else if (Subtarget->isTargetELF()) {
1069 // Emit final debug information.
1070 DW.EndModule();
1071 }
1072
1073 return AsmPrinter::doFinalization(M);
1074}
1075
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001076// Include the auto-generated portion of the assembly writer.
1077#include "X86GenAsmWriter.inc"