blob: 41afdb17dccc0481b9d0f00f2d535dd8da53a2ee [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
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000170void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000171 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000172
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000173 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000174
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +0000175 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000176
Evan Chenge22e62b2008-03-25 22:29:46 +0000177 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000178 switch (F->getLinkage()) {
179 default: assert(0 && "Unknown linkage type!");
180 case Function::InternalLinkage: // Symbols default to internal.
Evan Chenge22e62b2008-03-25 22:29:46 +0000181 EmitAlignment(FnAlign, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000182 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000183 case Function::DLLExportLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000184 case Function::ExternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000185 EmitAlignment(FnAlign, F);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000186 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000187 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000188 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000189 case Function::WeakLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000190 EmitAlignment(FnAlign, F);
Jim Laskeyea348582006-07-27 02:05:13 +0000191 if (Subtarget->isTargetDarwin()) {
Evan Chengf1616da2006-02-22 23:59:57 +0000192 O << "\t.globl\t" << CurrentFnName << "\n";
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000193 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000194 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman4e8e8312007-10-05 15:54:58 +0000195 O << "\t.globl\t" << CurrentFnName << "\n";
Anton Korobeynikov66413092007-02-23 01:58:50 +0000196 O << "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000197 } else {
Dan Gohman825811d2007-07-30 15:08:02 +0000198 O << "\t.weak\t" << CurrentFnName << "\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000199 }
200 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000201 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000202 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000203 if (const char *Directive = TAI->getHiddenDirective())
204 O << Directive << CurrentFnName << "\n";
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000205 } else if (F->hasProtectedVisibility()) {
206 if (const char *Directive = TAI->getProtectedDirective())
207 O << Directive << CurrentFnName << "\n";
208 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000209
210 if (Subtarget->isTargetELF())
Dan Gohman825811d2007-07-30 15:08:02 +0000211 O << "\t.type\t" << CurrentFnName << ",@function\n";
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000212 else if (Subtarget->isTargetCygMing()) {
213 O << "\t.def\t " << CurrentFnName
214 << ";\t.scl\t" <<
215 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
216 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
217 << ";\t.endef\n";
218 }
219
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000220 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000221 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000222 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000223 (F->getLinkage() == Function::LinkOnceLinkage ||
224 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000225 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000226}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000227
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000228/// runOnMachineFunction - This uses the printMachineInstruction()
229/// method to print assembly for each instruction.
230///
231bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
232 const Function *F = MF.getFunction();
233 unsigned CC = F->getCallingConv();
234
235 if (TAI->doesSupportDebugInformation()) {
236 // Let PassManager know we need debug information and relay
237 // the MachineModuleInfo address on to DwarfWriter.
238 MMI = &getAnalysis<MachineModuleInfo>();
239 DW.SetModuleInfo(MMI);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000240 }
241
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000242 SetupMachineFunction(MF);
243 O << "\n\n";
244
245 // Populate function information map. Actually, We don't want to populate
246 // non-stdcall or non-fastcall functions' information right now.
247 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
248 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
249
250 // Print out constants referenced by the function
251 EmitConstantPool(MF.getConstantPool());
252
253 if (F->hasDLLExportLinkage())
254 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
255
256 // Print the 'header' of function
257 emitFunctionHeader(MF);
258
259 // Emit pre-function debug and/or EH information.
260 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
261 DW.BeginFunction(&MF);
262
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000263 // Print out code for the function.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000264 bool hasAnyRealCode = false;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000265 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
266 I != E; ++I) {
267 // Print a label for the basic block.
Dan Gohmancb406c22007-10-03 19:26:29 +0000268 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000269 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000270 O << '\n';
271 }
Bill Wendling824a7212008-01-26 09:03:52 +0000272 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
273 II != IE; ++II) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274 // Print the assembly for the instruction.
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000275 if (II->getOpcode() != X86::LABEL)
276 hasAnyRealCode = true;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000277 printMachineInstruction(II);
278 }
279 }
Evan Cheng67afece2006-08-28 22:14:16 +0000280
Dale Johannesenf2247cf2008-04-08 00:37:56 +0000281 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
282 // If the function is empty, then we need to emit *something*. Otherwise,
283 // the function's label might be associated with something that it wasn't
284 // meant to be associated with. We emit a noop in this situation.
285 // We are assuming inline asms are code.
286 O << "\tnop\n";
287 }
288
Jim Laskey563321a2006-09-06 18:34:40 +0000289 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmana9f64342007-08-01 14:42:30 +0000290 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000291
Anton Korobeynikov1ecf0ee2008-06-28 11:09:01 +0000292 // Emit post-function debug information.
293 if (TAI->doesSupportDebugInformation())
Jim Laskey99db0442006-03-23 18:09:44 +0000294 DW.EndFunction();
Evan Cheng3c992d22006-03-07 02:02:57 +0000295
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000296 // Print out jump tables referenced by the function.
297 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000298
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000299 // We didn't modify anything.
300 return false;
301}
302
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000303static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000304 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
305}
306
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000307static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
308 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
309 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
310}
311
312static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Evan Chengae19abc2007-01-18 22:27:12 +0000313 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
314}
315
Chris Lattnera3b8c572006-02-06 23:41:19 +0000316void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000317 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000318 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000319 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000320 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000321 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000322 "Virtual registers should not make it this far!");
323 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000324 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000325 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000326 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000327 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
328 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000329 Reg = getX86SubSuperRegister(Reg, VT);
330 }
Evan Chengda47e6e2008-03-15 00:03:38 +0000331 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000332 O << (char)tolower(*Name);
333 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000334 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000335
Chris Lattner63b3d712006-05-04 17:21:20 +0000336 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000337 if (!Modifier ||
338 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000339 O << '$';
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000340 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000341 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000342 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000343 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000344 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000345 case MachineOperand::MO_JumpTableIndex: {
346 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
347 if (!isMemOp) O << '$';
Evan Cheng347d39f2007-10-14 05:57:21 +0000348 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000349 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000350
351 if (TM.getRelocationModel() == Reloc::PIC_) {
352 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000353 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
354 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000355 else if (Subtarget->isPICStyleGOT())
356 O << "@GOTOFF";
357 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000358
Evan Chengae19abc2007-01-18 22:27:12 +0000359 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000360 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000361 return;
362 }
Evan Chenga09bd812006-02-26 08:28:12 +0000363 case MachineOperand::MO_ConstantPoolIndex: {
364 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
365 if (!isMemOp) O << '$';
Evan Cheng347d39f2007-10-14 05:57:21 +0000366 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000367 << MO.getIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000368
369 if (TM.getRelocationModel() == Reloc::PIC_) {
370 if (Subtarget->isPICStyleStub())
Evan Cheng347d39f2007-10-14 05:57:21 +0000371 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
372 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000373 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000374 O << "@GOTOFF";
375 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000376
Evan Chenga09bd812006-02-26 08:28:12 +0000377 int Offset = MO.getOffset();
378 if (Offset > 0)
379 O << "+" << Offset;
380 else if (Offset < 0)
381 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000382
Evan Chengae19abc2007-01-18 22:27:12 +0000383 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000384 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000385 return;
386 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000387 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000388 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000389 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000390 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000391
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000392 const GlobalValue *GV = MO.getGlobal();
393 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
394 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000395 // If GV is an alias then use the aliasee for determining
396 // thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000397 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
398 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
399 }
400
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000401 bool isThreadLocal = GVar && GVar->isThreadLocal();
402
Evan Cheng25ab6902006-09-08 06:48:29 +0000403 std::string Name = Mang->getValueName(GV);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000404 decorateName(Name, GV);
Anton Korobeynikov91364382008-06-28 11:08:09 +0000405
Dan Gohman2a3250c2007-04-26 21:07:05 +0000406 if (!isMemOp && !isCallOp)
407 O << '$';
408 else if (Name[0] == '$') {
409 // The name begins with a dollar-sign. In order to avoid having it look
410 // like an integer immediate to the assembler, enclose it in parens.
411 O << '(';
412 needCloseParen = true;
413 }
414
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000415 if (shouldPrintStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000416 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000417 // non-lazily-resolved stubs
Reid Spencer5cbf9852007-01-30 20:08:39 +0000418 if (GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000419 GV->hasWeakLinkage() ||
Dale Johannesenaafce772008-05-14 20:12:51 +0000420 GV->hasLinkOnceLinkage() ||
421 GV->hasCommonLinkage()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000422 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000423 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000424 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000425 printSuffixedName(Name, "$stub");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000426 } else {
427 GVStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000428 printSuffixedName(Name, "$non_lazy_ptr");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000429 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000430 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000431 if (GV->hasDLLImportLinkage())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000432 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000433 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000434 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000435
Chris Lattner35d86fe2006-07-26 21:12:04 +0000436 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0475ab52008-01-05 00:41:47 +0000437 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000438 } else {
439 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikov91364382008-06-28 11:08:09 +0000440 O << "__imp_";
441 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000442 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000443
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000444 if (isCallOp) {
445 if (shouldPrintPLT(TM, Subtarget)) {
446 // Assemble call via PLT for externally visible symbols
447 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
448 !GV->hasInternalLinkage())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000449 O << "@PLT";
450 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000451 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000452 // Save function name for later type emission
453 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000454 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000455 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000456
Evan Cheng6d7d3102006-12-01 07:38:23 +0000457 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000458 ExtWeakSymbols.insert(GV);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000459
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000460 int Offset = MO.getOffset();
461 if (Offset > 0)
462 O << "+" << Offset;
463 else if (Offset < 0)
464 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000465
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000466 if (isThreadLocal) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000467 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000468 O << "@TLSGD"; // general dynamic TLS model
469 else
470 if (GV->isDeclaration())
471 O << "@INDNTPOFF"; // initial exec TLS model
472 else
473 O << "@NTPOFF"; // local exec TLS model
474 } else if (isMemOp) {
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000475 if (shouldPrintGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000476 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000477 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000478 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000479 O << "@GOTOFF";
Chris Lattnerfe6575c2007-11-04 19:23:28 +0000480 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
481 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov99e635c2008-01-20 13:59:37 +0000482 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Evan Chengae19abc2007-01-18 22:27:12 +0000483 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000484
485 if (needCloseParen) {
486 needCloseParen = false;
487 O << ')';
488 }
489
Evan Chengae19abc2007-01-18 22:27:12 +0000490 // Use rip when possible to reduce code size, except when
491 // index or base register are also part of the address. e.g.
492 // foo(%rip)(%rcx,%rax,4) is not legal
493 O << "(%rip)";
494 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000495 }
496
Dan Gohman2a3250c2007-04-26 21:07:05 +0000497 if (needCloseParen)
498 O << ')';
499
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000500 return;
501 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000502 case MachineOperand::MO_ExternalSymbol: {
503 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000504 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000505 std::string Name(TAI->getGlobalPrefix());
506 Name += MO.getSymbolName();
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000507 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000508 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000509 printSuffixedName(Name, "$stub");
Nate Begeman72b286b2005-07-08 00:23:26 +0000510 return;
511 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000512 if (!isCallOp)
513 O << '$';
514 else if (Name[0] == '$') {
515 // The name begins with a dollar-sign. In order to avoid having it look
516 // like an integer immediate to the assembler, enclose it in parens.
517 O << '(';
518 needCloseParen = true;
519 }
520
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000521 O << Name;
522
Rafael Espindolad674b4e2008-06-09 09:52:31 +0000523 if (shouldPrintPLT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000524 std::string GOTName(TAI->getGlobalPrefix());
525 GOTName+="_GLOBAL_OFFSET_TABLE_";
526 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000527 // HACK! Emit extra offset to PC during printing GOT offset to
528 // compensate for the size of popl instruction. The resulting code
529 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000530 // call .piclabel
531 // piclabel:
532 // popl %some_register
533 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000534 O << " + [.-"
Evan Cheng0475ab52008-01-05 00:41:47 +0000535 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << "]";
Evan Chengae19abc2007-01-18 22:27:12 +0000536
537 if (isCallOp)
538 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000539 }
540
Dan Gohman2a3250c2007-04-26 21:07:05 +0000541 if (needCloseParen)
542 O << ')';
543
Evan Chengae19abc2007-01-18 22:27:12 +0000544 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000545 O << "(%rip)";
546
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000547 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000548 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000549 default:
550 O << "<unknown operand type>"; return;
551 }
552}
553
Nate Begeman391c5d22005-11-30 18:54:35 +0000554void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000555 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000556 assert(value <= 7 && "Invalid ssecc argument!");
557 switch (value) {
558 case 0: O << "eq"; break;
559 case 1: O << "lt"; break;
560 case 2: O << "le"; break;
561 case 3: O << "unord"; break;
562 case 4: O << "neq"; break;
563 case 5: O << "nlt"; break;
564 case 6: O << "nle"; break;
565 case 7: O << "ord"; break;
566 }
567}
568
Evan Cheng25ab6902006-09-08 06:48:29 +0000569void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
570 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000571 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000572 MachineOperand BaseReg = MI->getOperand(Op);
573 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000574 const MachineOperand &DispSpec = MI->getOperand(Op+3);
575
Evan Cheng28b514392006-12-05 19:50:18 +0000576 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000577 if (DispSpec.isGlobalAddress() ||
578 DispSpec.isConstantPoolIndex() ||
579 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000580 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000581 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000582 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000583 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
584 O << DispVal;
585 }
586
587 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000588 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattner32c9a452007-01-14 00:13:07 +0000589 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000590
Chris Lattner32c9a452007-01-14 00:13:07 +0000591 // There are cases where we can end up with ESP/RSP in the indexreg slot.
592 // If this happens, swap the base/index register to support assemblers that
593 // don't work when the index is *SP.
594 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
595 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
596 std::swap(BaseReg, IndexReg);
597 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000598 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000599
Chris Lattner32c9a452007-01-14 00:13:07 +0000600 O << "(";
601 if (BaseReg.getReg())
602 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000603
604 if (IndexReg.getReg()) {
605 O << ",";
Chris Lattner32c9a452007-01-14 00:13:07 +0000606 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000607 if (ScaleVal != 1)
608 O << "," << ScaleVal;
609 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000610 O << ")";
611 }
612}
613
Anton Korobeynikov91364382008-06-28 11:08:09 +0000614void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000615 const MachineBasicBlock *MBB) const {
616 if (!TAI->getSetDirective())
617 return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000618
619 // We don't need .set machinery if we have GOT-style relocations
620 if (Subtarget->isPICStyleGOT())
621 return;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000622
Evan Chengcc415862007-11-09 01:32:10 +0000623 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
624 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000625 printBasicBlockLabel(MBB, false, false, false);
Evan Chenged2fc712007-11-09 19:11:23 +0000626 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikov91364382008-06-28 11:08:09 +0000627 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Chenged2fc712007-11-09 19:11:23 +0000628 << '_' << uid << '\n';
629 else
Evan Cheng0475ab52008-01-05 00:41:47 +0000630 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Chengcc415862007-11-09 01:32:10 +0000631}
632
Evan Cheng7ccced62006-02-18 00:15:05 +0000633void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0475ab52008-01-05 00:41:47 +0000634 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000635 O << label << "\n" << label << ":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000636}
637
Evan Cheng62f27002006-04-28 23:11:40 +0000638
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000639void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
640 const MachineBasicBlock *MBB,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000641 unsigned uid) const
642{
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000643 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
644 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
645
646 O << JTEntryDirective << ' ';
647
648 if (TM.getRelocationModel() == Reloc::PIC_) {
649 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
650 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
651 << '_' << uid << "_set_" << MBB->getNumber();
652 } else if (Subtarget->isPICStyleGOT()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000653 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000654 O << "@GOTOFF";
655 } else
656 assert(0 && "Don't know how to print MBB label for this PIC mode");
657 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000658 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000659}
660
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000661bool X86ATTAsmPrinter::PrintAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000662 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000663 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000664 switch (Mode) {
665 default: return true; // Unknown mode.
666 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000667 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000668 break;
669 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000670 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000671 break;
672 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000673 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000674 break;
675 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000676 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000677 break;
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000678 case 'q': // Print DImode register
679 Reg = getX86SubSuperRegister(Reg, MVT::i64);
680 break;
Evan Cheng62f27002006-04-28 23:11:40 +0000681 }
682
Evan Cheng8f7f7122006-05-05 05:40:20 +0000683 O << '%';
Evan Chengda47e6e2008-03-15 00:03:38 +0000684 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Evan Cheng8f7f7122006-05-05 05:40:20 +0000685 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000686 return false;
687}
688
Evan Cheng3d48a902006-04-28 21:19:05 +0000689/// PrintAsmOperand - Print out an operand for an inline asm expression.
690///
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000691bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000692 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000693 const char *ExtraCode) {
694 // Does this asm operand have a single letter operand modifier?
695 if (ExtraCode && ExtraCode[0]) {
696 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000697
Evan Cheng3d48a902006-04-28 21:19:05 +0000698 switch (ExtraCode[0]) {
699 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000700 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000701 printOperand(MI, OpNo, "mem");
702 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000703 case 'b': // Print QImode register
704 case 'h': // Print QImode high register
705 case 'w': // Print HImode register
706 case 'k': // Print SImode register
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000707 case 'q': // Print DImode register
Dan Gohman92dfe202007-09-14 20:33:02 +0000708 if (MI->getOperand(OpNo).isRegister())
Chris Lattner14393522007-03-25 02:01:03 +0000709 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
710 printOperand(MI, OpNo);
711 return false;
Anton Korobeynikov91364382008-06-28 11:08:09 +0000712
Chris Lattner7cd5e072007-03-25 01:44:57 +0000713 case 'P': // Don't print @PLT, but do print as memory.
714 printOperand(MI, OpNo, "mem");
715 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000716 }
717 }
Anton Korobeynikov91364382008-06-28 11:08:09 +0000718
Evan Cheng3d48a902006-04-28 21:19:05 +0000719 printOperand(MI, OpNo);
720 return false;
721}
722
Anton Korobeynikov31f98152008-06-28 11:09:17 +0000723bool X86ATTAsmPrinter::printAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng3d48a902006-04-28 21:19:05 +0000724 unsigned OpNo,
Anton Korobeynikov91364382008-06-28 11:08:09 +0000725 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000726 const char *ExtraCode) {
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000727 if (ExtraCode && ExtraCode[0]) {
728 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov91364382008-06-28 11:08:09 +0000729
Chris Lattnerfb7f3432007-10-29 03:09:07 +0000730 switch (ExtraCode[0]) {
731 default: return true; // Unknown modifier.
732 case 'b': // Print QImode register
733 case 'h': // Print QImode high register
734 case 'w': // Print HImode register
735 case 'k': // Print SImode register
736 case 'q': // Print SImode register
737 // These only apply to registers, ignore on mem.
738 break;
739 }
740 }
Evan Cheng3d48a902006-04-28 21:19:05 +0000741 printMemReference(MI, OpNo);
742 return false;
743}
744
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000745/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000746/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000747///
748void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
749 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000750
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000751 // Call the autogenerated instruction printer routines.
752 printInstruction(MI);
753}
754
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000755/// doInitialization
756bool X86ATTAsmPrinter::doInitialization(Module &M) {
757 if (TAI->doesSupportDebugInformation()) {
758 // Emit initial debug information.
759 DW.BeginModule(&M);
760 }
761
762 bool Result = AsmPrinter::doInitialization(M);
763
764 // Darwin wants symbols to be quoted if they have complex names.
765 if (Subtarget->isTargetDarwin())
766 Mang->setUseQuotes(true);
767
768 return Result;
769}
770
771
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000772void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000773 const TargetData *TD = TM.getTargetData();
774
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000775 if (!GVar->hasInitializer())
776 return; // External global require no code
777
778 // Check to see if this is a special global used by LLVM, if so, emit it.
779 if (EmitSpecialLLVMGlobal(GVar)) {
780 if (Subtarget->isTargetDarwin() &&
781 TM.getRelocationModel() == Reloc::Static) {
782 if (GVar->getName() == "llvm.global_ctors")
783 O << ".reference .constructors_used\n";
784 else if (GVar->getName() == "llvm.global_dtors")
785 O << ".reference .destructors_used\n";
786 }
787 return;
788 }
789
790 std::string name = Mang->getValueName(GVar);
791 Constant *C = GVar->getInitializer();
792 const Type *Type = C->getType();
793 unsigned Size = TD->getABITypeSize(Type);
794 unsigned Align = TD->getPreferredAlignmentLog(GVar);
795
796 if (GVar->hasHiddenVisibility()) {
797 if (const char *Directive = TAI->getHiddenDirective())
798 O << Directive << name << "\n";
799 } else if (GVar->hasProtectedVisibility()) {
800 if (const char *Directive = TAI->getProtectedDirective())
801 O << Directive << name << "\n";
802 }
803
804 if (Subtarget->isTargetELF())
805 O << "\t.type\t" << name << ",@object\n";
806
807 if (C->isNullValue() && !GVar->hasSection()) {
808 if (GVar->hasExternalLinkage()) {
809 if (const char *Directive = TAI->getZeroFillDirective()) {
810 O << "\t.globl " << name << "\n";
811 O << Directive << "__DATA, __common, " << name << ", "
812 << Size << ", " << Align << "\n";
813 return;
814 }
815 }
816
817 if (!GVar->isThreadLocal() &&
818 (GVar->hasInternalLinkage() || GVar->hasWeakLinkage() ||
819 GVar->hasLinkOnceLinkage() || GVar->hasCommonLinkage())) {
820 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
821 if (!NoZerosInBSS && TAI->getBSSSection())
822 SwitchToDataSection(TAI->getBSSSection(), GVar);
823 else
824 SwitchToDataSection(TAI->getDataSection(), GVar);
825 if (TAI->getLCOMMDirective() != NULL) {
826 if (GVar->hasInternalLinkage()) {
827 O << TAI->getLCOMMDirective() << name << "," << Size;
828 if (Subtarget->isTargetDarwin())
829 O << "," << Align;
830 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
831 O << "\t.globl " << name << "\n"
832 << TAI->getWeakDefDirective() << name << "\n";
833 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
834 EmitAlignment(Align, GVar);
835 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
836 PrintUnmangledNameSafely(GVar, O);
837 O << "\n";
838 EmitGlobalConstant(C);
839 return;
840 } else {
841 O << TAI->getCOMMDirective() << name << "," << Size;
842
843 // Leopard and above support aligned common symbols.
844 if (Subtarget->getDarwinVers() >= 9)
845 O << "," << Align;
846 }
847 } else {
848 if (!Subtarget->isTargetCygMing()) {
849 if (GVar->hasInternalLinkage())
850 O << "\t.local\t" << name << "\n";
851 }
852 O << TAI->getCOMMDirective() << name << "," << Size;
853 if (TAI->getCOMMDirectiveTakesAlignment())
854 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
855 }
856 O << "\t\t" << TAI->getCommentString() << " ";
857 PrintUnmangledNameSafely(GVar, O);
858 O << "\n";
859 return;
860 }
861 }
862
863 switch (GVar->getLinkage()) {
864 case GlobalValue::CommonLinkage:
865 case GlobalValue::LinkOnceLinkage:
866 case GlobalValue::WeakLinkage:
867 if (Subtarget->isTargetDarwin()) {
868 O << "\t.globl " << name << "\n"
869 << TAI->getWeakDefDirective() << name << "\n";
870 if (!GVar->isConstant())
871 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", GVar);
872 else {
873 const ArrayType *AT = dyn_cast<ArrayType>(Type);
874 if (AT && AT->getElementType()==Type::Int8Ty)
875 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", GVar);
876 else
877 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", GVar);
878 }
879 } else if (Subtarget->isTargetCygMing()) {
880 std::string SectionName(".section\t.data$linkonce." +
881 name +
882 ",\"aw\"");
883 SwitchToDataSection(SectionName.c_str(), GVar);
884 O << "\t.globl\t" << name << "\n"
885 << "\t.linkonce same_size\n";
886 } else {
887 std::string SectionName("\t.section\t.llvm.linkonce.d." +
888 name +
889 ",\"aw\",@progbits");
890 SwitchToDataSection(SectionName.c_str(), GVar);
891 O << "\t.weak\t" << name << "\n";
892 }
893 break;
894 case GlobalValue::DLLExportLinkage:
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000895 case GlobalValue::AppendingLinkage:
896 // FIXME: appending linkage variables should go into a section of
897 // their name or something. For now, just emit them as external.
898 case GlobalValue::ExternalLinkage:
899 // If external or appending, declare as a global symbol
900 O << "\t.globl " << name << "\n";
901 // FALL THROUGH
902 case GlobalValue::InternalLinkage: {
903 if (GVar->isConstant()) {
904 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
905 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
906 SwitchToDataSection(TAI->getCStringSection(), GVar);
907 break;
908 }
909 }
910 // FIXME: special handling for ".ctors" & ".dtors" sections
911 if (GVar->hasSection() &&
912 (GVar->getSection() == ".ctors" || GVar->getSection() == ".dtors")) {
913 std::string SectionName = ".section " + GVar->getSection();
914
915 if (Subtarget->isTargetCygMing()) {
916 SectionName += ",\"aw\"";
917 } else {
918 assert(!Subtarget->isTargetDarwin());
919 SectionName += ",\"aw\",@progbits";
920 }
921 SwitchToDataSection(SectionName.c_str());
922 } else if (GVar->hasSection() && Subtarget->isTargetDarwin()) {
923 // Honor all section names on Darwin; ObjC uses this
924 std::string SectionName = ".section " + GVar->getSection();
925 SwitchToDataSection(SectionName.c_str());
926 } else {
927 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
928 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSBSSSection() :
929 TAI->getBSSSection(), GVar);
930 else if (!GVar->isConstant())
931 SwitchToDataSection(GVar->isThreadLocal() ? TAI->getTLSDataSection() :
932 TAI->getDataSection(), GVar);
933 else if (GVar->isThreadLocal())
934 SwitchToDataSection(TAI->getTLSDataSection());
935 else {
936 // Read-only data.
937 bool HasReloc = C->ContainsRelocations();
938 if (HasReloc &&
939 Subtarget->isTargetDarwin() &&
940 TM.getRelocationModel() != Reloc::Static)
941 SwitchToDataSection("\t.const_data\n");
942 else if (!HasReloc && Size == 4 &&
943 TAI->getFourByteConstantSection())
944 SwitchToDataSection(TAI->getFourByteConstantSection(), GVar);
945 else if (!HasReloc && Size == 8 &&
946 TAI->getEightByteConstantSection())
947 SwitchToDataSection(TAI->getEightByteConstantSection(), GVar);
948 else if (!HasReloc && Size == 16 &&
949 TAI->getSixteenByteConstantSection())
950 SwitchToDataSection(TAI->getSixteenByteConstantSection(), GVar);
951 else if (TAI->getReadOnlySection())
952 SwitchToDataSection(TAI->getReadOnlySection(), GVar);
953 else
954 SwitchToDataSection(TAI->getDataSection(), GVar);
955 }
956 }
957
958 break;
959 }
960 default:
961 assert(0 && "Unknown linkage type!");
962 }
963
964 EmitAlignment(Align, GVar);
965 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
966 PrintUnmangledNameSafely(GVar, O);
967 O << "\n";
968 if (TAI->hasDotTypeDotSizeDirective())
969 O << "\t.size\t" << name << ", " << Size << "\n";
970
971 // If the initializer is a extern weak symbol, remember to emit the weak
972 // reference!
973 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
974 if (GV->hasExternalWeakLinkage())
975 ExtWeakSymbols.insert(GV);
976
977 EmitGlobalConstant(C);
978}
979
980
981bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000982 // Print out module-level global variables here.
983 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000984 I != E; ++I) {
Anton Korobeynikove51ab442008-06-28 11:09:32 +0000985 printModuleLevelGV(I);
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000986
Anton Korobeynikovf0302cd2008-06-28 11:09:48 +0000987 if (I->hasDLLExportLinkage())
988 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
989 }
990
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000991 // Output linker support code for dllexported globals
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000992 if (!DLLExportedGVs.empty())
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000993 SwitchToDataSection(".section .drectve");
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000994
995 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
996 e = DLLExportedGVs.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +0000997 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000998 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +0000999
1000 if (!DLLExportedFns.empty()) {
1001 SwitchToDataSection(".section .drectve");
1002 }
1003
1004 for (StringSet<>::iterator i = DLLExportedFns.begin(),
1005 e = DLLExportedFns.end();
Anton Korobeynikov36a57012008-06-28 11:08:44 +00001006 i != e; ++i)
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001007 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikov75b68822008-06-28 11:08:27 +00001008
1009 if (Subtarget->isTargetDarwin()) {
1010 SwitchToDataSection("");
1011
1012 // Output stubs for dynamically-linked functions
1013 unsigned j = 1;
1014 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1015 i != e; ++i, ++j) {
1016 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1017 "self_modifying_code+pure_instructions,5", 0);
1018 std::string p = i->getKeyData();
1019 printSuffixedName(p, "$stub");
1020 O << ":\n";
1021 O << "\t.indirect_symbol " << p << "\n";
1022 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1023 }
1024
1025 O << "\n";
1026
1027 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1028 // Add the (possibly multiple) personalities to the set of global values.
1029 // Only referenced functions get into the Personalities list.
1030 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1031
1032 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1033 E = Personalities.end(); I != E; ++I)
1034 if (*I) GVStubs.insert("_" + (*I)->getName());
1035 }
1036
1037 // Output stubs for external and common global variables.
1038 if (!GVStubs.empty())
1039 SwitchToDataSection(
1040 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1041 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1042 i != e; ++i) {
1043 std::string p = i->getKeyData();
1044 printSuffixedName(p, "$non_lazy_ptr");
1045 O << ":\n";
1046 O << "\t.indirect_symbol " << p << "\n";
1047 O << "\t.long\t0\n";
1048 }
1049
1050 // Emit final debug information.
1051 DW.EndModule();
1052
1053 // Funny Darwin hack: This flag tells the linker that no global symbols
1054 // contain code that falls through to other global symbols (e.g. the obvious
1055 // implementation of multiple entry points). If this doesn't occur, the
1056 // linker can safely perform dead code stripping. Since LLVM never
1057 // generates code that does this, it is always safe to set.
1058 O << "\t.subsections_via_symbols\n";
1059 } else if (Subtarget->isTargetCygMing()) {
1060 // Emit type information for external functions
1061 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1062 i != e; ++i) {
1063 O << "\t.def\t " << i->getKeyData()
1064 << ";\t.scl\t" << COFF::C_EXT
1065 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1066 << ";\t.endef\n";
1067 }
1068
1069 // Emit final debug information.
1070 DW.EndModule();
1071 } else if (Subtarget->isTargetELF()) {
1072 // Emit final debug information.
1073 DW.EndModule();
1074 }
1075
1076 return AsmPrinter::doFinalization(M);
1077}
1078
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001079// Include the auto-generated portion of the assembly writer.
1080#include "X86GenAsmWriter.inc"