blob: 4a9002c13b4c9084fbd6040c37d6492702c7f1fa [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
Chris Lattnerafbfded2006-10-05 02:43:52 +0000150/// getSectionForFunction - Return the section that we should emit the
151/// specified function body into.
152std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
153 switch (F.getLinkage()) {
154 default: assert(0 && "Unknown linkage type!");
Anton Korobeynikov91364382008-06-28 11:08:09 +0000155 case Function::InternalLinkage:
Chris Lattnerafbfded2006-10-05 02:43:52 +0000156 case Function::DLLExportLinkage:
157 case Function::ExternalLinkage:
158 return TAI->getTextSection();
159 case Function::WeakLinkage:
160 case Function::LinkOnceLinkage:
161 if (Subtarget->isTargetDarwin()) {
162 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000163 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmanaf67ea72007-06-14 15:00:27 +0000164 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
Chris Lattnerafbfded2006-10-05 02:43:52 +0000165 } else {
166 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
Dan Gohmanaf67ea72007-06-14 15:00:27 +0000167 ",\"ax\",@progbits";
Chris Lattnerafbfded2006-10-05 02:43:52 +0000168 }
169 }
170}
171
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000172void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000173 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000174
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000175 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000176
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +0000177 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000178
Evan Chenge22e62b2008-03-25 22:29:46 +0000179 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000180 switch (F->getLinkage()) {
181 default: assert(0 && "Unknown linkage type!");
182 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000183 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000184 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000185 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000186 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000187 EmitAlignment(FnAlign, F);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000188 O << "\t.globl\t" << CurrentFnName << '\n';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000189 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000190 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000191 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000192 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000193 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000194 O << "\t.globl\t" << CurrentFnName << '\n';
195 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000196 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000197 O << "\t.globl\t" << CurrentFnName << "\n"
198 "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000199 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000200 O << "\t.weak\t" << CurrentFnName << '\n';
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000201 }
202 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000203 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000204 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000205 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000206 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000207 } else if (F->hasProtectedVisibility()) {
208 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000209 O << Directive << CurrentFnName << '\n';
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000210 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000211
212 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000213 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000214 else if (Subtarget->isTargetCygMing()) {
215 O << "\t.def\t " << CurrentFnName
216 << ";\t.scl\t" <<
217 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
218 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
219 << ";\t.endef\n";
220 }
221
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000222 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000223 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000224 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000225 (F->getLinkage() == Function::LinkOnceLinkage ||
226 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000227 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000228}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000229
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000230/// runOnMachineFunction - This uses the printMachineInstruction()
231/// method to print assembly for each instruction.
232///
233bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
234 const Function *F = MF.getFunction();
235 unsigned CC = F->getCallingConv();
236
Evan Cheng526be702008-07-09 06:36:53 +0000237 if (TAI->doesSupportDebugInformation()) {
238 // Let PassManager know we need debug information and relay
239 // the MachineModuleInfo address on to DwarfWriter.
240 MMI = &getAnalysis<MachineModuleInfo>();
241 DW.SetModuleInfo(MMI);
242 }
243
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000244 SetupMachineFunction(MF);
245 O << "\n\n";
246
247 // Populate function information map. Actually, We don't want to populate
248 // non-stdcall or non-fastcall functions' information right now.
249 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
250 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
251
252 // Print out constants referenced by the function
253 EmitConstantPool(MF.getConstantPool());
254
255 if (F->hasDLLExportLinkage())
256 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
257
258 // Print the 'header' of function
259 emitFunctionHeader(MF);
260
261 // Emit pre-function debug and/or EH information.
262 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
263 DW.BeginFunction(&MF);
264
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000265 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000266 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000267 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
268 I != E; ++I) {
269 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000270 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000271 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000272 O << '\n';
273 }
Bill Wendling824a7212008-01-26 09:03:52 +0000274 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
275 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000276 // Print the assembly for the instruction.
Dan Gohman44066042008-07-01 00:05:16 +0000277 if (!II->isLabel())
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000278 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000279 printMachineInstruction(II);
280 }
281 }
Evan Cheng67afece2006-08-28 22:14:16 +0000282
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000283 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
284 // If the function is empty, then we need to emit *something*. Otherwise,
285 // the function's label might be associated with something that it wasn't
286 // meant to be associated with. We emit a noop in this situation.
287 // We are assuming inline asms are code.
288 O << "\tnop\n";
289 }
290
Jim Laskey563321a2006-09-06 18:34:40 +0000291 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000292 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000293
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000294 // Emit post-function debug information.
295 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000296 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000297
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000298 // Print out jump tables referenced by the function.
299 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000300
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000301 // We didn't modify anything.
302 return false;
303}
304
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000305static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000306 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
307}
308
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000309static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
310 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
311 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
312}
313
314static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000315 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
316}
317
Chris Lattnera3b8c572006-02-06 23:41:19 +0000318void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000319 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000320 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000321 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000322 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000323 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000324 "Virtual registers should not make it this far!");
325 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000326 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000327 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000328 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000329 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
330 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000331 Reg = getX86SubSuperRegister(Reg, VT);
332 }
Evan Chengae270f62008-07-07 22:21:06 +0000333 O << TRI->getAsmName(Reg);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000334 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000335 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000336
Chris Lattner63b3d712006-05-04 17:21:20 +0000337 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000338 if (!Modifier ||
339 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000340 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000341 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000342 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000343 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000344 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000345 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000346 case MachineOperand::MO_JumpTableIndex: {
347 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
348 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000349 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000350 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000351
352 if (TM.getRelocationModel() == Reloc::PIC_) {
353 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000354 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
355 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000356 else if (Subtarget->isPICStyleGOT())
357 O << "@GOTOFF";
358 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000359
Evan Chengae19abc2007-01-18 22:27:12 +0000360 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000361 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000362 return;
363 }
Evan Chenga09bd812006-02-26 08:28:12 +0000364 case MachineOperand::MO_ConstantPoolIndex: {
365 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
366 if (!isMemOp) O << '$';
Dan Gohmand19a53b2008-06-30 22:03:41 +0000367 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner8aa797a2007-12-30 23:10:15 +0000368 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000369
370 if (TM.getRelocationModel() == Reloc::PIC_) {
371 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000372 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
373 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000374 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000375 O << "@GOTOFF";
376 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000377
Evan Chenga09bd812006-02-26 08:28:12 +0000378 int Offset = MO.getOffset();
379 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000380 O << '+' << Offset;
Evan Chenga09bd812006-02-26 08:28:12 +0000381 else if (Offset < 0)
382 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000383
Evan Chengae19abc2007-01-18 22:27:12 +0000384 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000385 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000386 return;
387 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000388 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000389 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000390 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000391 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000392
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000393 const GlobalValue *GV = MO.getGlobal();
394 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
395 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000396 // If GV is an alias then use the aliasee for determining
397 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000398 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
399 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
400 }
401
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000402 bool isThreadLocal = GVar && GVar->isThreadLocal();
403
Evan Cheng25ab6902006-09-08 06:48:29 +0000404 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000405 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000406
Dan Gohman2a3250c2007-04-26 21:07:05 +0000407 if (!isMemOp && !isCallOp)
408 O << '$';
409 else if (Name[0] == '$') {
410 // The name begins with a dollar-sign. In order to avoid having it look
411 // like an integer immediate to the assembler, enclose it in parens.
412 O << '(';
413 needCloseParen = true;
414 }
415
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000416 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000417 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000418 // non-lazily-resolved stubs
Reid Spencer5cbf9852007-01-30 20:08:39 +0000419 if (GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000420 GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000421 GV->hasLinkOnceLinkage() ||
422 GV->hasCommonLinkage()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000423 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000424 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000425 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000426 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000427 } else {
428 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000429 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000430 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000431 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000432 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000433 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000434 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000435 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000436
Chris Lattner35d86fe2006-07-26 21:12:04 +0000437 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000438 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000439 } else {
440 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000441 O << "__imp_";
442 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000443 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000444
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000445 if (isCallOp) {
446 if (shouldPrintPLT(TM, Subtarget)) {
447 // Assemble call via PLT for externally visible symbols
448 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
449 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000450 O << "@PLT";
451 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000452 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000453 // Save function name for later type emission
454 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000455 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000456 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000457
Evan Cheng6d7d3102006-12-01 07:38:23 +0000458 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000459 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000460
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000461 int Offset = MO.getOffset();
462 if (Offset > 0)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000463 O << '+' << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000464 else if (Offset < 0)
465 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000466
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000467 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000468 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000469 O << "@TLSGD"; // general dynamic TLS model
470 else
471 if (GV->isDeclaration())
472 O << "@INDNTPOFF"; // initial exec TLS model
473 else
474 O << "@NTPOFF"; // local exec TLS model
475 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000476 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000477 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000478 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000479 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000480 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000481 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
482 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000483 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000484 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000485
486 if (needCloseParen) {
487 needCloseParen = false;
488 O << ')';
489 }
490
Evan Chengae19abc2007-01-18 22:27:12 +0000491 // Use rip when possible to reduce code size, except when
492 // index or base register are also part of the address. e.g.
493 // foo(%rip)(%rcx,%rax,4) is not legal
494 O << "(%rip)";
495 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000496 }
497
Dan Gohman2a3250c2007-04-26 21:07:05 +0000498 if (needCloseParen)
499 O << ')';
500
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000501 return;
502 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000503 case MachineOperand::MO_ExternalSymbol: {
504 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000505 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000506 std::string Name(TAI->getGlobalPrefix());
507 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000508 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000509 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000510 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000511 return;
512 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000513 if (!isCallOp)
514 O << '$';
515 else if (Name[0] == '$') {
516 // The name begins with a dollar-sign. In order to avoid having it look
517 // like an integer immediate to the assembler, enclose it in parens.
518 O << '(';
519 needCloseParen = true;
520 }
521
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000522 O << Name;
523
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000524 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000525 std::string GOTName(TAI->getGlobalPrefix());
526 GOTName+="_GLOBAL_OFFSET_TABLE_";
527 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000528 // HACK! Emit extra offset to PC during printing GOT offset to
529 // compensate for the size of popl instruction. The resulting code
530 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000531 // call .piclabel
532 // piclabel:
533 // popl %some_register
534 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000535 O << " + [.-"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000536 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Evan Chengae19abc2007-01-18 22:27:12 +0000537
538 if (isCallOp)
539 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000540 }
541
Dan Gohman2a3250c2007-04-26 21:07:05 +0000542 if (needCloseParen)
543 O << ')';
544
Evan Chengae19abc2007-01-18 22:27:12 +0000545 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000546 O << "(%rip)";
547
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000548 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000549 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000550 default:
551 O << "<unknown operand type>"; return;
552 }
553}
554
Nate Begeman391c5d22005-11-30 18:54:35 +0000555void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000556 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000557 assert(value <= 7 && "Invalid ssecc argument!");
558 switch (value) {
559 case 0: O << "eq"; break;
560 case 1: O << "lt"; break;
561 case 2: O << "le"; break;
562 case 3: O << "unord"; break;
563 case 4: O << "neq"; break;
564 case 5: O << "nlt"; break;
565 case 6: O << "nle"; break;
566 case 7: O << "ord"; break;
567 }
568}
569
Evan Cheng25ab6902006-09-08 06:48:29 +0000570void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
571 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000572 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000573 MachineOperand BaseReg = MI->getOperand(Op);
574 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000575 const MachineOperand &DispSpec = MI->getOperand(Op+3);
576
Evan Cheng28b514392006-12-05 19:50:18 +0000577 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000578 if (DispSpec.isGlobalAddress() ||
579 DispSpec.isConstantPoolIndex() ||
580 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000581 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000582 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000583 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000584 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
585 O << DispVal;
586 }
587
588 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000589 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000590 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000591
Chris Lattner32c9a452007-01-14 00:13:07 +0000592 // There are cases where we can end up with ESP/RSP in the indexreg slot.
593 // If this happens, swap the base/index register to support assemblers that
594 // don't work when the index is *SP.
595 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
596 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
597 std::swap(BaseReg, IndexReg);
598 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000599 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000600
Dan Gohmand19a53b2008-06-30 22:03:41 +0000601 O << '(';
Chris Lattner32c9a452007-01-14 00:13:07 +0000602 if (BaseReg.getReg())
603 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000604
605 if (IndexReg.getReg()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000606 O << ',';
Chris Lattner32c9a452007-01-14 00:13:07 +0000607 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000608 if (ScaleVal != 1)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000609 O << ',' << ScaleVal;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000610 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000611 O << ')';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000612 }
613}
614
Anton Korobeynikov91364382008-06-28 11:08:09 +0000615void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000616 const MachineBasicBlock *MBB) const {
617 if (!TAI->getSetDirective())
618 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000619
620 // We don't need .set machinery if we have GOT-style relocations
621 if (Subtarget->isPICStyleGOT())
622 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000623
Evan Chengcc415862007-11-09 01:32:10 +0000624 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
625 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000626 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000627 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000628 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000629 << '_' << uid << '\n';
630 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000631 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000632}
633
Evan Cheng7ccced62006-02-18 00:15:05 +0000634void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000635 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000636 O << label << '\n' << label << ':';
Evan Cheng7ccced62006-02-18 00:15:05 +0000637}
638
Evan Cheng62f27002006-04-28 23:11:40 +0000639
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000640void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
641 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000642 unsigned uid) const
643{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000644 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
645 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
646
647 O << JTEntryDirective << ' ';
648
649 if (TM.getRelocationModel() == Reloc::PIC_) {
650 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
651 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
652 << '_' << uid << "_set_" << MBB->getNumber();
653 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000654 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000655 O << "@GOTOFF";
656 } else
657 assert(0 && "Don't know how to print MBB label for this PIC mode");
658 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000659 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000660}
661
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000662bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000663 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000664 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000665 switch (Mode) {
666 default: return true; // Unknown mode.
667 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000668 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000669 break;
670 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000671 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000672 break;
673 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000674 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000675 break;
676 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000677 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000678 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000679 case 'q': // Print DImode register
680 Reg = getX86SubSuperRegister(Reg, MVT::i64);
681 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000682 }
683
Evan Chengae270f62008-07-07 22:21:06 +0000684 O << '%'<< TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000685 return false;
686}
687
Evan Cheng3d48a902006-04-28 21:19:05 +0000688/// PrintAsmOperand - Print out an operand for an inline asm expression.
689///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000690bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000691 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000692 const char *ExtraCode) {
693 // Does this asm operand have a single letter operand modifier?
694 if (ExtraCode && ExtraCode[0]) {
695 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000696
Evan Cheng3d48a902006-04-28 21:19:05 +0000697 switch (ExtraCode[0]) {
698 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000699 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000700 printOperand(MI, OpNo, "mem");
701 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000702 case 'b': // Print QImode register
703 case 'h': // Print QImode high register
704 case 'w': // Print HImode register
705 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000706 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000707 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000708 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
709 printOperand(MI, OpNo);
710 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000711
Chris Lattner7cd5e072007-03-25 01:44:57 +0000712 case 'P': // Don't print @PLT, but do print as memory.
713 printOperand(MI, OpNo, "mem");
714 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000715 }
716 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000717
Evan Cheng3d48a902006-04-28 21:19:05 +0000718 printOperand(MI, OpNo);
719 return false;
720}
721
Anton Korobeynikov4d580652008-06-28 11:10:06 +0000722bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000723 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000724 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000725 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000726 if (ExtraCode && ExtraCode[0]) {
727 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000728
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000729 switch (ExtraCode[0]) {
730 default: return true; // Unknown modifier.
731 case 'b': // Print QImode register
732 case 'h': // Print QImode high register
733 case 'w': // Print HImode register
734 case 'k': // Print SImode register
735 case 'q': // Print SImode register
736 // These only apply to registers, ignore on mem.
737 break;
738 }
739 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000740 printMemReference(MI, OpNo);
741 return false;
742}
743
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000744/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000745/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000746///
747void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
748 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000749
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000750 // Call the autogenerated instruction printer routines.
751 printInstruction(MI);
752}
753
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000754/// doInitialization
755bool X86ATTAsmPrinter::doInitialization(Module &M) {
756 if (TAI->doesSupportDebugInformation()) {
757 // Emit initial debug information.
758 DW.BeginModule(&M);
759 }
760
Evan Cheng526be702008-07-09 06:36:53 +0000761 bool Result = AsmPrinter::doInitialization(M);
762
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000763 // Darwin wants symbols to be quoted if they have complex names.
764 if (Subtarget->isTargetDarwin())
765 Mang->setUseQuotes(true);
766
767 return Result;
768}
769
770
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000771void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000772 const TargetData *TD = TM.getTargetData();
773
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000774 if (!GVar->hasInitializer())
775 return; // External global require no code
776
Anton Korobeynikov0e48a0c2008-07-09 13:24:38 +0000777 GVar->dump();
778 std::cout << TAI->SectionForGlobal(GVar) << std::endl;
779
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000780 // Check to see if this is a special global used by LLVM, if so, emit it.
781 if (EmitSpecialLLVMGlobal(GVar)) {
782 if (Subtarget->isTargetDarwin() &&
783 TM.getRelocationModel() == Reloc::Static) {
784 if (GVar->getName() == "llvm.global_ctors")
785 O << ".reference .constructors_used\n";
786 else if (GVar->getName() == "llvm.global_dtors")
787 O << ".reference .destructors_used\n";
788 }
789 return;
790 }
791
792 std::string name = Mang->getValueName(GVar);
793 Constant *C = GVar->getInitializer();
794 const Type *Type = C->getType();
795 unsigned Size = TD->getABITypeSize(Type);
796 unsigned Align = TD->getPreferredAlignmentLog(GVar);
797
798 if (GVar->hasHiddenVisibility()) {
799 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000800 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000801 } else if (GVar->hasProtectedVisibility()) {
802 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000803 O << Directive << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000804 }
805
806 if (Subtarget->isTargetELF())
807 O << "\t.type\t" << name << ",@object\n";
808
809 if (C->isNullValue() && !GVar->hasSection()) {
810 if (GVar->hasExternalLinkage()) {
811 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000812 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000813 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohmand19a53b2008-06-30 22:03:41 +0000814 << Size << ", " << Align << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000815 return;
816 }
817 }
818
819 if (!GVar->isThreadLocal() &&
820 (GVar->hasInternalLinkage() || GVar->hasWeakLinkage() ||
821 GVar->hasLinkOnceLinkage() || GVar->hasCommonLinkage())) {
822 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
823 if (!NoZerosInBSS && TAI->getBSSSection())
824 SwitchToDataSection(TAI->getBSSSection(), GVar);
825 else
826 SwitchToDataSection(TAI->getDataSection(), GVar);
827 if (TAI->getLCOMMDirective() != NULL) {
828 if (GVar->hasInternalLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000829 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000830 if (Subtarget->isTargetDarwin())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000831 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000832 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000833 O << "\t.globl " << name << '\n'
834 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000835 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
836 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000837 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000838 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000839 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000840 EmitGlobalConstant(C);
841 return;
842 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000843 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000844
845 // Leopard and above support aligned common symbols.
846 if (Subtarget->getDarwinVers() >= 9)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000847 O << ',' << Align;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000848 }
849 } else {
850 if (!Subtarget->isTargetCygMing()) {
851 if (GVar->hasInternalLinkage())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000852 O << "\t.local\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000853 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000854 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000855 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000856 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000857 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000858 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000859 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000860 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000861 return;
862 }
863 }
864
865 switch (GVar->getLinkage()) {
866 case GlobalValue::CommonLinkage:
867 case GlobalValue::LinkOnceLinkage:
868 case GlobalValue::WeakLinkage:
869 if (Subtarget->isTargetDarwin()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000870 O << "\t.globl " << name << '\n'
871 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000872 if (!GVar->isConstant())
873 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
874 else {
875 const ArrayType *AT = dyn_cast<ArrayType>(Type);
876 if (AT && AT->getElementType()==Type::Int8Ty)
877 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", GVar);
878 else
879 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", GVar);
880 }
881 } else if (Subtarget->isTargetCygMing()) {
882 std::string SectionName(".section\t.data$linkonce." +
883 name +
884 ",\"aw\"");
885 SwitchToDataSection(SectionName.c_str(), GVar);
886 O << "\t.globl\t" << name << "\n"
Dan Gohmand19a53b2008-06-30 22:03:41 +0000887 "\t.linkonce same_size\n";
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000888 } else {
889 std::string SectionName("\t.section\t.llvm.linkonce.d." +
890 name +
891 ",\"aw\",@progbits");
892 SwitchToDataSection(SectionName.c_str(), GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000893 O << "\t.weak\t" << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000894 }
895 break;
896 case GlobalValue::DLLExportLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000897 case GlobalValue::AppendingLinkage:
898 // FIXME: appending linkage variables should go into a section of
899 // their name or something. For now, just emit them as external.
900 case GlobalValue::ExternalLinkage:
901 // If external or appending, declare as a global symbol
Dan Gohmand19a53b2008-06-30 22:03:41 +0000902 O << "\t.globl " << name << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000903 // FALL THROUGH
904 case GlobalValue::InternalLinkage: {
905 if (GVar->isConstant()) {
906 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
907 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
908 SwitchToDataSection(TAI->getCStringSection(), GVar);
909 break;
910 }
911 }
912 // FIXME: special handling for ".ctors" & ".dtors" sections
913 if (GVar->hasSection() &&
914 (GVar->getSection() == ".ctors" || GVar->getSection() == ".dtors")) {
915 std::string SectionName = ".section " + GVar->getSection();
916
917 if (Subtarget->isTargetCygMing()) {
918 SectionName += ",\"aw\"";
919 } else {
920 assert(!Subtarget->isTargetDarwin());
921 SectionName += ",\"aw\",@progbits";
922 }
923 SwitchToDataSection(SectionName.c_str());
924 } else if (GVar->hasSection() && Subtarget->isTargetDarwin()) {
925 // Honor all section names on Darwin; ObjC uses this
926 std::string SectionName = ".section " + GVar->getSection();
927 SwitchToDataSection(SectionName.c_str());
928 } else {
929 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
930 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSBSSSection() :
931 TAI->getBSSSection(), GVar);
932 else if (!GVar->isConstant())
933 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSDataSection() :
934 TAI->getDataSection(), GVar);
935 else if (GVar->isThreadLocal())
936 SwitchToDataSection(TAI->getTLSDataSection());
937 else {
938 // Read-only data.
939 bool HasReloc = C->ContainsRelocations();
940 if (HasReloc &&
941 Subtarget->isTargetDarwin() &&
942 TM.getRelocationModel() != Reloc::Static)
943 SwitchToDataSection("\t.const_data\n");
944 else if (!HasReloc && Size == 4 &&
945 TAI->getFourByteConstantSection())
946 SwitchToDataSection(TAI->getFourByteConstantSection(), GVar);
947 else if (!HasReloc && Size == 8 &&
948 TAI->getEightByteConstantSection())
949 SwitchToDataSection(TAI->getEightByteConstantSection(), GVar);
950 else if (!HasReloc && Size == 16 &&
951 TAI->getSixteenByteConstantSection())
952 SwitchToDataSection(TAI->getSixteenByteConstantSection(), GVar);
953 else if (TAI->getReadOnlySection())
954 SwitchToDataSection(TAI->getReadOnlySection(), GVar);
955 else
956 SwitchToDataSection(TAI->getDataSection(), GVar);
957 }
958 }
959
960 break;
961 }
962 default:
963 assert(0 && "Unknown linkage type!");
964 }
965
966 EmitAlignment(Align, GVar);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000967 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000968 PrintUnmangledNameSafely(GVar, O);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000969 O << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000970 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000971 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000972
973 // If the initializer is a extern weak symbol, remember to emit the weak
974 // reference!
975 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
976 if (GV->hasExternalWeakLinkage())
977 ExtWeakSymbols.insert(GV);
978
979 EmitGlobalConstant(C);
980}
981
Evan Cheng77c8f762008-07-08 00:55:58 +0000982/// printGVStub - Print stub for a global value.
983///
984void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Chengab8faba2008-07-08 16:40:43 +0000985 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng77c8f762008-07-08 00:55:58 +0000986 O << ":\n\t.indirect_symbol ";
987 if (Prefix) O << Prefix;
988 O << GV << "\n\t.long\t0\n";
989}
990
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000991
992bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000993 // Print out module-level global variables here.
994 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000995 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000996 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000997
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000998 if (I->hasDLLExportLinkage())
999 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
1000 }
1001
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001002 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001003 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001004 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001005
1006 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
1007 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001008 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001009 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001010
1011 if (!DLLExportedFns.empty()) {
1012 SwitchToDataSection(".section .drectve");
1013 }
1014
1015 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1016 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001017 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001018 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001019
1020 if (Subtarget->isTargetDarwin()) {
1021 SwitchToDataSection("");
1022
1023 // Output stubs for dynamically-linked functions
1024 unsigned j = 1;
1025 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1026 i != e; ++i, ++j) {
1027 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1028 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng77c8f762008-07-08 00:55:58 +00001029 const char *p = i->getKeyData();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001030 printSuffixedName(p, "$stub");
Dan Gohmand19a53b2008-06-30 22:03:41 +00001031 O << ":\n"
1032 "\t.indirect_symbol " << p << "\n"
1033 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001034 }
1035
Dan Gohmand19a53b2008-06-30 22:03:41 +00001036 O << '\n';
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001037
Evan Cheng77c8f762008-07-08 00:55:58 +00001038 // Print global value stubs.
1039 bool InStubSection = false;
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001040 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1041 // Add the (possibly multiple) personalities to the set of global values.
1042 // Only referenced functions get into the Personalities list.
1043 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001044 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng77c8f762008-07-08 00:55:58 +00001045 E = Personalities.end(); I != E; ++I) {
1046 if (!*I)
1047 continue;
1048 if (!InStubSection) {
1049 SwitchToDataSection(
1050 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1051 InStubSection = true;
1052 }
1053 printGVStub((*I)->getNameStart(), "_");
1054 }
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001055 }
1056
1057 // Output stubs for external and common global variables.
Evan Cheng77c8f762008-07-08 00:55:58 +00001058 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001059 SwitchToDataSection(
1060 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1061 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng77c8f762008-07-08 00:55:58 +00001062 i != e; ++i)
1063 printGVStub(i->getKeyData());
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001064
1065 // Emit final debug information.
1066 DW.EndModule();
1067
1068 // Funny Darwin hack: This flag tells the linker that no global symbols
1069 // contain code that falls through to other global symbols (e.g. the obvious
1070 // implementation of multiple entry points). If this doesn't occur, the
1071 // linker can safely perform dead code stripping. Since LLVM never
1072 // generates code that does this, it is always safe to set.
1073 O << "\t.subsections_via_symbols\n";
1074 } else if (Subtarget->isTargetCygMing()) {
1075 // Emit type information for external functions
1076 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1077 i != e; ++i) {
1078 O << "\t.def\t " << i->getKeyData()
1079 << ";\t.scl\t" << COFF::C_EXT
1080 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1081 << ";\t.endef\n";
1082 }
1083
1084 // Emit final debug information.
1085 DW.EndModule();
1086 } else if (Subtarget->isTargetELF()) {
1087 // Emit final debug information.
1088 DW.EndModule();
1089 }
1090
1091 return AsmPrinter::doFinalization(M);
1092}
1093
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001094// Include the auto-generated portion of the assembly writer.
1095#include "X86GenAsmWriter.inc"