blob: 69914e57c24a365b3afa163bb0fea7fa899502d0 [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())
Evan Cheng0475ab52008-01-05 00:41:47 +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
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000170/// runOnMachineFunction - This uses the printMachineInstruction()
171/// method to print assembly for each instruction.
172///
173bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000174 if (TAI->doesSupportDebugInformation()) {
Jim Laskeye29c2f52006-07-19 11:54:50 +0000175 // Let PassManager know we need debug information and relay
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000176 // the MachineModuleInfo address on to DwarfWriter.
Bill Wendlingd60da492007-09-11 08:27:17 +0000177 MMI = &getAnalysis<MachineModuleInfo>();
178 DW.SetModuleInfo(MMI);
Jim Laskeye29c2f52006-07-19 11:54:50 +0000179 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000180
Chris Lattner8b8b9512005-11-21 07:51:23 +0000181 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000182 O << "\n\n";
183
184 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000185 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000186
187 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000188 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000189 unsigned CC = F->getCallingConv();
190
191 // Populate function information map. Actually, We don't want to populate
192 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +0000193 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +0000194 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000195
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000196 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000197
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +0000198 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000199
Evan Chenge22e62b2008-03-25 22:29:46 +0000200 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000201 switch (F->getLinkage()) {
202 default: assert(0 && "Unknown linkage type!");
203 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000204 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000205 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000206 case Function::DLLExportLinkage:
207 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
208 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +0000209 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000210 EmitAlignment(FnAlign, F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000211 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000212 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000213 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000214 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000215 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000216 if (Subtarget->isTargetDarwin()) {
Evan Chengf1616da2006-02-22 23:59:57 +0000217 O << "\t.globl\t" << CurrentFnName << "\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000218 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000219 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman4e8e8312007-10-05 15:54:58 +0000220 O << "\t.globl\t" << CurrentFnName << "\n";
Anton Korobeynikov66413092007-02-23 01:58:50 +0000221 O << "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000222 } else {
Dan Gohman825811d2007-07-30 15:08:02 +0000223 O << "\t.weak\t" << CurrentFnName << "\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000224 }
225 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000226 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000227 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000228 if (const char *Directive = TAI->getHiddenDirective())
229 O << Directive << CurrentFnName << "\n";
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000230 } else if (F->hasProtectedVisibility()) {
231 if (const char *Directive = TAI->getProtectedDirective())
232 O << Directive << CurrentFnName << "\n";
233 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000234
235 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000236 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000237 else if (Subtarget->isTargetCygMing()) {
238 O << "\t.def\t " << CurrentFnName
239 << ";\t.scl\t" <<
240 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
241 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
242 << ";\t.endef\n";
243 }
244
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000246 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000247 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000248 (F->getLinkage() == Function::LinkOnceLinkage ||
249 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000250 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000251
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000252 if (TAI->doesSupportDebugInformation() ||
253 TAI->doesSupportExceptionHandling()) {
254 // Emit pre-function debug and/or EH information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000255 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000256 }
257
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000258 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000259 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
261 I != E; ++I) {
262 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000263 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000264 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000265 O << '\n';
266 }
Bill Wendling824a7212008-01-26 09:03:52 +0000267 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
268 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000269 // Print the assembly for the instruction.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000270 if (II->getOpcode() != X86::LABEL)
271 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000272 printMachineInstruction(II);
273 }
274 }
Evan Cheng67afece2006-08-28 22:14:16 +0000275
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000276 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
277 // If the function is empty, then we need to emit *something*. Otherwise,
278 // the function's label might be associated with something that it wasn't
279 // meant to be associated with. We emit a noop in this situation.
280 // We are assuming inline asms are code.
281 O << "\tnop\n";
282 }
283
Jim Laskey563321a2006-09-06 18:34:40 +0000284 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmana9f64342007-08-01 14:42:30 +0000285 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000286
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000287 if (TAI->doesSupportDebugInformation()) {
Evan Chengd5948812006-03-07 02:23:26 +0000288 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000289 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000290 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000291
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000292 // Print out jump tables referenced by the function.
293 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000294
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000295 // We didn't modify anything.
296 return false;
297}
298
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000299static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000300 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
301}
302
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000303static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
304 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
305 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
306}
307
308static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000309 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
310}
311
Chris Lattnera3b8c572006-02-06 23:41:19 +0000312void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000313 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000314 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000315 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000316 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000317 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000318 "Virtual registers should not make it this far!");
319 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000320 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000321 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000322 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000323 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
324 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000325 Reg = getX86SubSuperRegister(Reg, VT);
326 }
Evan Chengda47e6e2008-03-15 00:03:38 +0000327 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000328 O << (char)tolower(*Name);
329 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000330 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000331
Chris Lattner63b3d712006-05-04 17:21:20 +0000332 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000333 if (!Modifier ||
334 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000335 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000336 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000337 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000338 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000339 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000340 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000341 case MachineOperand::MO_JumpTableIndex: {
342 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
343 if (!isMemOp) O << '$';
Evan Cheng347d39f2007-10-14 05:57:21 +0000344 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000345 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000346
347 if (TM.getRelocationModel() == Reloc::PIC_) {
348 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000349 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
350 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000351 else if (Subtarget->isPICStyleGOT())
352 O << "@GOTOFF";
353 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000354
Evan Chengae19abc2007-01-18 22:27:12 +0000355 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000356 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000357 return;
358 }
Evan Chenga09bd812006-02-26 08:28:12 +0000359 case MachineOperand::MO_ConstantPoolIndex: {
360 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
361 if (!isMemOp) O << '$';
Evan Cheng347d39f2007-10-14 05:57:21 +0000362 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000363 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000364
365 if (TM.getRelocationModel() == Reloc::PIC_) {
366 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000367 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
368 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000369 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000370 O << "@GOTOFF";
371 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000372
Evan Chenga09bd812006-02-26 08:28:12 +0000373 int Offset = MO.getOffset();
374 if (Offset > 0)
375 O << "+" << Offset;
376 else if (Offset < 0)
377 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000378
Evan Chengae19abc2007-01-18 22:27:12 +0000379 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000380 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000381 return;
382 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000383 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000384 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000385 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000386 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000387
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000388 const GlobalValue *GV = MO.getGlobal();
389 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
390 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000391 // If GV is an alias then use the aliasee for determining
392 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000393 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
394 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
395 }
396
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000397 bool isThreadLocal = GVar && GVar->isThreadLocal();
398
Evan Cheng25ab6902006-09-08 06:48:29 +0000399 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000400 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000401
Dan Gohman2a3250c2007-04-26 21:07:05 +0000402 if (!isMemOp && !isCallOp)
403 O << '$';
404 else if (Name[0] == '$') {
405 // The name begins with a dollar-sign. In order to avoid having it look
406 // like an integer immediate to the assembler, enclose it in parens.
407 O << '(';
408 needCloseParen = true;
409 }
410
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000411 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000412 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000413 // non-lazily-resolved stubs
Reid Spencer5cbf9852007-01-30 20:08:39 +0000414 if (GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000415 GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000416 GV->hasLinkOnceLinkage() ||
417 GV->hasCommonLinkage()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000418 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000419 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000420 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000421 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000422 } else {
423 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000424 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000425 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000426 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000427 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000428 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000429 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000430 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000431
Chris Lattner35d86fe2006-07-26 21:12:04 +0000432 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000433 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000434 } else {
435 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000436 O << "__imp_";
437 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000438 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000439
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000440 if (isCallOp) {
441 if (shouldPrintPLT(TM, Subtarget)) {
442 // Assemble call via PLT for externally visible symbols
443 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
444 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000445 O << "@PLT";
446 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000447 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000448 // Save function name for later type emission
449 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000450 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000451 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000452
Evan Cheng6d7d3102006-12-01 07:38:23 +0000453 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000454 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000455
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000456 int Offset = MO.getOffset();
457 if (Offset > 0)
458 O << "+" << Offset;
459 else if (Offset < 0)
460 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000461
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000462 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000463 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000464 O << "@TLSGD"; // general dynamic TLS model
465 else
466 if (GV->isDeclaration())
467 O << "@INDNTPOFF"; // initial exec TLS model
468 else
469 O << "@NTPOFF"; // local exec TLS model
470 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000471 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000472 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000473 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000474 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000475 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000476 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
477 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000478 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000479 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000480
481 if (needCloseParen) {
482 needCloseParen = false;
483 O << ')';
484 }
485
Evan Chengae19abc2007-01-18 22:27:12 +0000486 // Use rip when possible to reduce code size, except when
487 // index or base register are also part of the address. e.g.
488 // foo(%rip)(%rcx,%rax,4) is not legal
489 O << "(%rip)";
490 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000491 }
492
Dan Gohman2a3250c2007-04-26 21:07:05 +0000493 if (needCloseParen)
494 O << ')';
495
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000496 return;
497 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000498 case MachineOperand::MO_ExternalSymbol: {
499 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000500 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000501 std::string Name(TAI->getGlobalPrefix());
502 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000503 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000504 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000505 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000506 return;
507 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000508 if (!isCallOp)
509 O << '$';
510 else if (Name[0] == '$') {
511 // The name begins with a dollar-sign. In order to avoid having it look
512 // like an integer immediate to the assembler, enclose it in parens.
513 O << '(';
514 needCloseParen = true;
515 }
516
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000517 O << Name;
518
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000519 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000520 std::string GOTName(TAI->getGlobalPrefix());
521 GOTName+="_GLOBAL_OFFSET_TABLE_";
522 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000523 // HACK! Emit extra offset to PC during printing GOT offset to
524 // compensate for the size of popl instruction. The resulting code
525 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000526 // call .piclabel
527 // piclabel:
528 // popl %some_register
529 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000530 O << " + [.-"
Evan Cheng0475ab52008-01-05 00:41:47 +0000531 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << "]";
Evan Chengae19abc2007-01-18 22:27:12 +0000532
533 if (isCallOp)
534 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000535 }
536
Dan Gohman2a3250c2007-04-26 21:07:05 +0000537 if (needCloseParen)
538 O << ')';
539
Evan Chengae19abc2007-01-18 22:27:12 +0000540 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000541 O << "(%rip)";
542
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000543 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000544 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000545 default:
546 O << "<unknown operand type>"; return;
547 }
548}
549
Nate Begeman391c5d22005-11-30 18:54:35 +0000550void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000551 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000552 assert(value <= 7 && "Invalid ssecc argument!");
553 switch (value) {
554 case 0: O << "eq"; break;
555 case 1: O << "lt"; break;
556 case 2: O << "le"; break;
557 case 3: O << "unord"; break;
558 case 4: O << "neq"; break;
559 case 5: O << "nlt"; break;
560 case 6: O << "nle"; break;
561 case 7: O << "ord"; break;
562 }
563}
564
Evan Cheng25ab6902006-09-08 06:48:29 +0000565void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
566 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000567 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000568 MachineOperand BaseReg = MI->getOperand(Op);
569 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000570 const MachineOperand &DispSpec = MI->getOperand(Op+3);
571
Evan Cheng28b514392006-12-05 19:50:18 +0000572 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000573 if (DispSpec.isGlobalAddress() ||
574 DispSpec.isConstantPoolIndex() ||
575 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000576 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000577 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000578 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000579 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
580 O << DispVal;
581 }
582
583 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000584 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000585 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000586
Chris Lattner32c9a452007-01-14 00:13:07 +0000587 // There are cases where we can end up with ESP/RSP in the indexreg slot.
588 // If this happens, swap the base/index register to support assemblers that
589 // don't work when the index is *SP.
590 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
591 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
592 std::swap(BaseReg, IndexReg);
593 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000594 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000595
Chris Lattner32c9a452007-01-14 00:13:07 +0000596 O << "(";
597 if (BaseReg.getReg())
598 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000599
600 if (IndexReg.getReg()) {
601 O << ",";
Chris Lattner32c9a452007-01-14 00:13:07 +0000602 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000603 if (ScaleVal != 1)
604 O << "," << ScaleVal;
605 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000606 O << ")";
607 }
608}
609
Anton Korobeynikov91364382008-06-28 11:08:09 +0000610void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000611 const MachineBasicBlock *MBB) const {
612 if (!TAI->getSetDirective())
613 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000614
615 // We don't need .set machinery if we have GOT-style relocations
616 if (Subtarget->isPICStyleGOT())
617 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000618
Evan Chengcc415862007-11-09 01:32:10 +0000619 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
620 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000621 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000622 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000623 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000624 << '_' << uid << '\n';
625 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000626 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000627}
628
Evan Cheng7ccced62006-02-18 00:15:05 +0000629void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000630 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000631 O << label << "\n" << label << ":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000632}
633
Evan Cheng62f27002006-04-28 23:11:40 +0000634
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000635void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
636 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000637 unsigned uid) const
638{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000639 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
640 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
641
642 O << JTEntryDirective << ' ';
643
644 if (TM.getRelocationModel() == Reloc::PIC_) {
645 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
646 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
647 << '_' << uid << "_set_" << MBB->getNumber();
648 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000649 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000650 O << "@GOTOFF";
651 } else
652 assert(0 && "Don't know how to print MBB label for this PIC mode");
653 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000654 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000655}
656
Evan Cheng55c25f22006-04-28 23:19:39 +0000657bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000658 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000659 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000660 switch (Mode) {
661 default: return true; // Unknown mode.
662 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000663 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000664 break;
665 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000666 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000667 break;
668 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000669 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000670 break;
671 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000672 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000673 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000674 case 'q': // Print DImode register
675 Reg = getX86SubSuperRegister(Reg, MVT::i64);
676 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000677 }
678
Evan Cheng8f7f7122006-05-05 05:40:20 +0000679 O << '%';
Evan Chengda47e6e2008-03-15 00:03:38 +0000680 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Evan Cheng8f7f7122006-05-05 05:40:20 +0000681 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000682 return false;
683}
684
Evan Cheng3d48a902006-04-28 21:19:05 +0000685/// PrintAsmOperand - Print out an operand for an inline asm expression.
686///
687bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000688 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000689 const char *ExtraCode) {
690 // Does this asm operand have a single letter operand modifier?
691 if (ExtraCode && ExtraCode[0]) {
692 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000693
Evan Cheng3d48a902006-04-28 21:19:05 +0000694 switch (ExtraCode[0]) {
695 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000696 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000697 printOperand(MI, OpNo, "mem");
698 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000699 case 'b': // Print QImode register
700 case 'h': // Print QImode high register
701 case 'w': // Print HImode register
702 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000703 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000704 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000705 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
706 printOperand(MI, OpNo);
707 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000708
Chris Lattner7cd5e072007-03-25 01:44:57 +0000709 case 'P': // Don't print @PLT, but do print as memory.
710 printOperand(MI, OpNo, "mem");
711 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000712 }
713 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000714
Evan Cheng3d48a902006-04-28 21:19:05 +0000715 printOperand(MI, OpNo);
716 return false;
717}
718
719bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
720 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000721 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000722 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000723 if (ExtraCode && ExtraCode[0]) {
724 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000725
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000726 switch (ExtraCode[0]) {
727 default: return true; // Unknown modifier.
728 case 'b': // Print QImode register
729 case 'h': // Print QImode high register
730 case 'w': // Print HImode register
731 case 'k': // Print SImode register
732 case 'q': // Print SImode register
733 // These only apply to registers, ignore on mem.
734 break;
735 }
736 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000737 printMemReference(MI, OpNo);
738 return false;
739}
740
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000741/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000742/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000743///
744void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
745 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000746
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000747 // Call the autogenerated instruction printer routines.
748 printInstruction(MI);
749}
750
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000751/// doInitialization
752bool X86ATTAsmPrinter::doInitialization(Module &M) {
753 if (TAI->doesSupportDebugInformation()) {
754 // Emit initial debug information.
755 DW.BeginModule(&M);
756 }
757
758 bool Result = AsmPrinter::doInitialization(M);
759
760 // Darwin wants symbols to be quoted if they have complex names.
761 if (Subtarget->isTargetDarwin())
762 Mang->setUseQuotes(true);
763
764 return Result;
765}
766
767
768bool X86ATTAsmPrinter::doFinalization(Module &M) {
769 // Note: this code is not shared by the Intel printer as it is too different
770 // from how MASM does things. When making changes here don't forget to look
771 // at X86IntelAsmPrinter::doFinalization().
772 const TargetData *TD = TM.getTargetData();
773
774 // Print out module-level global variables here.
775 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
776 I != E; ++I) {
777 if (!I->hasInitializer())
778 continue; // External global require no code
779
780 // Check to see if this is a special global used by LLVM, if so, emit it.
781 if (EmitSpecialLLVMGlobal(I)) {
782 if (Subtarget->isTargetDarwin() &&
783 TM.getRelocationModel() == Reloc::Static) {
784 if (I->getName() == "llvm.global_ctors")
785 O << ".reference .constructors_used\n";
786 else if (I->getName() == "llvm.global_dtors")
787 O << ".reference .destructors_used\n";
788 }
789 continue;
790 }
791
792 std::string name = Mang->getValueName(I);
793 Constant *C = I->getInitializer();
794 const Type *Type = C->getType();
795 unsigned Size = TD->getABITypeSize(Type);
796 unsigned Align = TD->getPreferredAlignmentLog(I);
797
798 if (I->hasHiddenVisibility()) {
799 if (const char *Directive = TAI->getHiddenDirective())
800 O << Directive << name << "\n";
801 } else if (I->hasProtectedVisibility()) {
802 if (const char *Directive = TAI->getProtectedDirective())
803 O << Directive << name << "\n";
804 }
805
806 if (Subtarget->isTargetELF())
807 O << "\t.type\t" << name << ",@object\n";
808
809 if (C->isNullValue() && !I->hasSection()) {
810 if (I->hasExternalLinkage()) {
811 if (const char *Directive = TAI->getZeroFillDirective()) {
812 O << "\t.globl " << name << "\n";
813 O << Directive << "__DATA, __common, " << name << ", "
814 << Size << ", " << Align << "\n";
815 continue;
816 }
817 }
818
819 if (!I->isThreadLocal() &&
820 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
821 I->hasLinkOnceLinkage() || I->hasCommonLinkage())) {
822 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
823 if (!NoZerosInBSS && TAI->getBSSSection())
824 SwitchToDataSection(TAI->getBSSSection(), I);
825 else
826 SwitchToDataSection(TAI->getDataSection(), I);
827 if (TAI->getLCOMMDirective() != NULL) {
828 if (I->hasInternalLinkage()) {
829 O << TAI->getLCOMMDirective() << name << "," << Size;
830 if (Subtarget->isTargetDarwin())
831 O << "," << Align;
832 } else if (Subtarget->isTargetDarwin() && !I->hasCommonLinkage()) {
833 O << "\t.globl " << name << "\n"
834 << TAI->getWeakDefDirective() << name << "\n";
835 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
836 EmitAlignment(Align, I);
837 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
838 PrintUnmangledNameSafely(I, O);
839 O << "\n";
840 EmitGlobalConstant(C);
841 continue;
842 } else {
843 O << TAI->getCOMMDirective() << name << "," << Size;
844
845 // Leopard and above support aligned common symbols.
846 if (Subtarget->getDarwinVers() >= 9)
847 O << "," << Align;
848 }
849 } else {
850 if (!Subtarget->isTargetCygMing()) {
851 if (I->hasInternalLinkage())
852 O << "\t.local\t" << name << "\n";
853 }
854 O << TAI->getCOMMDirective() << name << "," << Size;
855 if (TAI->getCOMMDirectiveTakesAlignment())
856 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
857 }
858 O << "\t\t" << TAI->getCommentString() << " ";
859 PrintUnmangledNameSafely(I, O);
860 O << "\n";
861 continue;
862 }
863 }
864
865 switch (I->getLinkage()) {
866 case GlobalValue::CommonLinkage:
867 case GlobalValue::LinkOnceLinkage:
868 case GlobalValue::WeakLinkage:
869 if (Subtarget->isTargetDarwin()) {
870 O << "\t.globl " << name << "\n"
871 << TAI->getWeakDefDirective() << name << "\n";
872 if (!I->isConstant())
873 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
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", I);
878 else
879 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
880 }
881 } else if (Subtarget->isTargetCygMing()) {
882 std::string SectionName(".section\t.data$linkonce." +
883 name +
884 ",\"aw\"");
885 SwitchToDataSection(SectionName.c_str(), I);
886 O << "\t.globl\t" << name << "\n"
887 << "\t.linkonce same_size\n";
888 } else {
889 std::string SectionName("\t.section\t.llvm.linkonce.d." +
890 name +
891 ",\"aw\",@progbits");
892 SwitchToDataSection(SectionName.c_str(), I);
893 O << "\t.weak\t" << name << "\n";
894 }
895 break;
896 case GlobalValue::DLLExportLinkage:
897 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
898 // FALL THROUGH
899 case GlobalValue::AppendingLinkage:
900 // FIXME: appending linkage variables should go into a section of
901 // their name or something. For now, just emit them as external.
902 case GlobalValue::ExternalLinkage:
903 // If external or appending, declare as a global symbol
904 O << "\t.globl " << name << "\n";
905 // FALL THROUGH
906 case GlobalValue::InternalLinkage: {
907 if (I->isConstant()) {
908 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
909 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
910 SwitchToDataSection(TAI->getCStringSection(), I);
911 break;
912 }
913 }
914 // FIXME: special handling for ".ctors" & ".dtors" sections
915 if (I->hasSection() &&
916 (I->getSection() == ".ctors" ||
917 I->getSection() == ".dtors")) {
918 std::string SectionName = ".section " + I->getSection();
919
920 if (Subtarget->isTargetCygMing()) {
921 SectionName += ",\"aw\"";
922 } else {
923 assert(!Subtarget->isTargetDarwin());
924 SectionName += ",\"aw\",@progbits";
925 }
926 SwitchToDataSection(SectionName.c_str());
927 } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
928 // Honor all section names on Darwin; ObjC uses this
929 std::string SectionName = ".section " + I->getSection();
930 SwitchToDataSection(SectionName.c_str());
931 } else {
932 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
933 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
934 TAI->getBSSSection(), I);
935 else if (!I->isConstant())
936 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
937 TAI->getDataSection(), I);
938 else if (I->isThreadLocal())
939 SwitchToDataSection(TAI->getTLSDataSection());
940 else {
941 // Read-only data.
942 bool HasReloc = C->ContainsRelocations();
943 if (HasReloc &&
944 Subtarget->isTargetDarwin() &&
945 TM.getRelocationModel() != Reloc::Static)
946 SwitchToDataSection("\t.const_data\n");
947 else if (!HasReloc && Size == 4 &&
948 TAI->getFourByteConstantSection())
949 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
950 else if (!HasReloc && Size == 8 &&
951 TAI->getEightByteConstantSection())
952 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
953 else if (!HasReloc && Size == 16 &&
954 TAI->getSixteenByteConstantSection())
955 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
956 else if (TAI->getReadOnlySection())
957 SwitchToDataSection(TAI->getReadOnlySection(), I);
958 else
959 SwitchToDataSection(TAI->getDataSection(), I);
960 }
961 }
962
963 break;
964 }
965 default:
966 assert(0 && "Unknown linkage type!");
967 }
968
969 EmitAlignment(Align, I);
970 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
971 PrintUnmangledNameSafely(I, O);
972 O << "\n";
973 if (TAI->hasDotTypeDotSizeDirective())
974 O << "\t.size\t" << name << ", " << Size << "\n";
975 // If the initializer is a extern weak symbol, remember to emit the weak
976 // reference!
977 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
978 if (GV->hasExternalWeakLinkage())
979 ExtWeakSymbols.insert(GV);
980
981 EmitGlobalConstant(C);
982 }
983
984 // Output linker support code for dllexported globals
985 if (!DLLExportedGVs.empty()) {
986 SwitchToDataSection(".section .drectve");
987 }
988
989 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
990 e = DLLExportedGVs.end();
991 i != e; ++i) {
992 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
993 }
994
995 if (!DLLExportedFns.empty()) {
996 SwitchToDataSection(".section .drectve");
997 }
998
999 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1000 e = DLLExportedFns.end();
1001 i != e; ++i) {
1002 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
1003 }
1004
1005 if (Subtarget->isTargetDarwin()) {
1006 SwitchToDataSection("");
1007
1008 // Output stubs for dynamically-linked functions
1009 unsigned j = 1;
1010 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1011 i != e; ++i, ++j) {
1012 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1013 "self_modifying_code+pure_instructions,5", 0);
1014 std::string p = i->getKeyData();
1015 printSuffixedName(p, "$stub");
1016 O << ":\n";
1017 O << "\t.indirect_symbol " << p << "\n";
1018 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1019 }
1020
1021 O << "\n";
1022
1023 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1024 // Add the (possibly multiple) personalities to the set of global values.
1025 // Only referenced functions get into the Personalities list.
1026 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1027
1028 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1029 E = Personalities.end(); I != E; ++I)
1030 if (*I) GVStubs.insert("_" + (*I)->getName());
1031 }
1032
1033 // Output stubs for external and common global variables.
1034 if (!GVStubs.empty())
1035 SwitchToDataSection(
1036 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1037 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1038 i != e; ++i) {
1039 std::string p = i->getKeyData();
1040 printSuffixedName(p, "$non_lazy_ptr");
1041 O << ":\n";
1042 O << "\t.indirect_symbol " << p << "\n";
1043 O << "\t.long\t0\n";
1044 }
1045
1046 // Emit final debug information.
1047 DW.EndModule();
1048
1049 // Funny Darwin hack: This flag tells the linker that no global symbols
1050 // contain code that falls through to other global symbols (e.g. the obvious
1051 // implementation of multiple entry points). If this doesn't occur, the
1052 // linker can safely perform dead code stripping. Since LLVM never
1053 // generates code that does this, it is always safe to set.
1054 O << "\t.subsections_via_symbols\n";
1055 } else if (Subtarget->isTargetCygMing()) {
1056 // Emit type information for external functions
1057 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1058 i != e; ++i) {
1059 O << "\t.def\t " << i->getKeyData()
1060 << ";\t.scl\t" << COFF::C_EXT
1061 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1062 << ";\t.endef\n";
1063 }
1064
1065 // Emit final debug information.
1066 DW.EndModule();
1067 } else if (Subtarget->isTargetELF()) {
1068 // Emit final debug information.
1069 DW.EndModule();
1070 }
1071
1072 return AsmPrinter::doFinalization(M);
1073}
1074
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001075// Include the auto-generated portion of the assembly writer.
1076#include "X86GenAsmWriter.inc"