blob: 0236e49288823a30a25274d2f08325c96e9f9a6b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
16#define DEBUG_TYPE "asm-printer"
17#include "X86ATTAsmPrinter.h"
18#include "X86.h"
19#include "X86COFF.h"
20#include "X86MachineFunctionInfo.h"
21#include "X86TargetMachine.h"
22#include "X86TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CallingConv.h"
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000024#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
Anton Korobeynikovab6c6a42008-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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.h"
31#include "llvm/Target/TargetAsmInfo.h"
32#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
Anton Korobeynikovb81503c2008-07-09 13:24:38 +000035#include <iostream>
36
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Evan Cheng0729ccf2008-01-05 00:41:47 +000039static std::string getPICLabelString(unsigned FnNum,
40 const TargetAsmInfo *TAI,
41 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 std::string label;
43 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000044 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 else if (Subtarget->isTargetELF())
Dan Gohman12ebe3f2008-06-30 22:03:41 +000046 label = ".Lllvm$" + utostr_32(FnNum) + "." "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 else
48 assert(0 && "Don't know how to print PIC label!\n");
49
50 return label;
51}
52
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +000053static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
54 const TargetData *TD) {
55 X86MachineFunctionInfo Info;
56 uint64_t Size = 0;
57
58 switch (F->getCallingConv()) {
59 case CallingConv::X86_StdCall:
60 Info.setDecorationStyle(StdCall);
61 break;
62 case CallingConv::X86_FastCall:
63 Info.setDecorationStyle(FastCall);
64 break;
65 default:
66 return Info;
67 }
68
69 unsigned argNum = 1;
70 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
71 AI != AE; ++AI, ++argNum) {
72 const Type* Ty = AI->getType();
73
74 // 'Dereference' type in case of byval parameter attribute
75 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
76 Ty = cast<PointerType>(Ty)->getElementType();
77
78 // Size should be aligned to DWORD boundary
79 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
80 }
81
82 // We're not supporting tooooo huge arguments :)
83 Info.setBytesToPopOnReturn((unsigned int)Size);
84 return Info;
85}
86
87/// PrintUnmangledNameSafely - Print out the printable characters in the name.
88/// Don't print things like \n or \0.
89static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
90 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
91 Name != E; ++Name)
92 if (isprint(*Name))
93 OS << *Name;
94}
95
96/// decorateName - Query FunctionInfoMap and use this information for various
97/// name decoration.
98void X86ATTAsmPrinter::decorateName(std::string &Name,
99 const GlobalValue *GV) {
100 const Function *F = dyn_cast<Function>(GV);
101 if (!F) return;
102
103 // We don't want to decorate non-stdcall or non-fastcall functions right now
104 unsigned CC = F->getCallingConv();
105 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
106 return;
107
108 // Decorate names only when we're targeting Cygwin/Mingw32 targets
109 if (!Subtarget->isTargetCygMing())
110 return;
111
112 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
113
114 const X86MachineFunctionInfo *Info;
115 if (info_item == FunctionInfoMap.end()) {
116 // Calculate apropriate function info and populate map
117 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
118 Info = &FunctionInfoMap[F];
119 } else {
120 Info = &info_item->second;
121 }
122
123 const FunctionType *FT = F->getFunctionType();
124 switch (Info->getDecorationStyle()) {
125 case None:
126 break;
127 case StdCall:
128 // "Pure" variadic functions do not receive @0 suffix.
129 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
131 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
132 break;
133 case FastCall:
134 // "Pure" variadic functions do not receive @0 suffix.
135 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
137 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
138
139 if (Name[0] == '_') {
140 Name[0] = '@';
141 } else {
142 Name = '@' + Name;
143 }
144 break;
145 default:
146 assert(0 && "Unsupported DecorationStyle");
147 }
148}
149
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000150// Substitute old hook with new one temporary
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000152 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153}
154
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000155void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 const Function *F = MF.getFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000158 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000160 SwitchToTextSection(TAI->SectionForGlobal(F).c_str(), F);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000161
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000162 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 switch (F->getLinkage()) {
164 default: assert(0 && "Unknown linkage type!");
165 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000166 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 break;
168 case Function::DLLExportLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000170 EmitAlignment(FnAlign, F);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000171 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 break;
173 case Function::LinkOnceLinkage:
174 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000175 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000177 O << "\t.globl\t" << CurrentFnName << '\n';
178 O << TAI->getWeakDefDirective() << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else if (Subtarget->isTargetCygMing()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000180 O << "\t.globl\t" << CurrentFnName << "\n"
181 "\t.linkonce discard\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000183 O << "\t.weak\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 }
185 break;
186 }
187 if (F->hasHiddenVisibility()) {
188 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000189 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 } else if (F->hasProtectedVisibility()) {
191 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000192 O << Directive << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 }
194
195 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000196 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 else if (Subtarget->isTargetCygMing()) {
198 O << "\t.def\t " << CurrentFnName
199 << ";\t.scl\t" <<
200 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
201 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
202 << ";\t.endef\n";
203 }
204
205 O << CurrentFnName << ":\n";
206 // Add some workaround for linkonce linkage on Cygwin\MinGW
207 if (Subtarget->isTargetCygMing() &&
208 (F->getLinkage() == Function::LinkOnceLinkage ||
209 F->getLinkage() == Function::WeakLinkage))
210 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000211}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000213/// runOnMachineFunction - This uses the printMachineInstruction()
214/// method to print assembly for each instruction.
215///
216bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
217 const Function *F = MF.getFunction();
218 unsigned CC = F->getCallingConv();
219
Evan Cheng5cda7762008-07-09 06:36:53 +0000220 if (TAI->doesSupportDebugInformation()) {
221 // Let PassManager know we need debug information and relay
222 // the MachineModuleInfo address on to DwarfWriter.
223 MMI = &getAnalysis<MachineModuleInfo>();
224 DW.SetModuleInfo(MMI);
225 }
226
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000227 SetupMachineFunction(MF);
228 O << "\n\n";
229
230 // Populate function information map. Actually, We don't want to populate
231 // non-stdcall or non-fastcall functions' information right now.
232 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
233 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
234
235 // Print out constants referenced by the function
236 EmitConstantPool(MF.getConstantPool());
237
238 if (F->hasDLLExportLinkage())
239 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
240
241 // Print the 'header' of function
242 emitFunctionHeader(MF);
243
244 // Emit pre-function debug and/or EH information.
245 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
246 DW.BeginFunction(&MF);
247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000249 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
251 I != E; ++I) {
252 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000253 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000254 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 O << '\n';
256 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000257 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
258 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 // Print the assembly for the instruction.
Dan Gohmanfa607c92008-07-01 00:05:16 +0000260 if (!II->isLabel())
Dale Johannesenf35771f2008-04-08 00:37:56 +0000261 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 printMachineInstruction(II);
263 }
264 }
265
Dale Johannesenf35771f2008-04-08 00:37:56 +0000266 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
267 // If the function is empty, then we need to emit *something*. Otherwise,
268 // the function's label might be associated with something that it wasn't
269 // meant to be associated with. We emit a noop in this situation.
270 // We are assuming inline asms are code.
271 O << "\tnop\n";
272 }
273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000275 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276
Anton Korobeynikov30948e32008-06-28 11:09:01 +0000277 // Emit post-function debug information.
278 if (TAI->doesSupportDebugInformation())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 DW.EndFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280
281 // Print out jump tables referenced by the function.
282 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000283
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 // We didn't modify anything.
285 return false;
286}
287
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000288static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
290}
291
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000292static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
293 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
294 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
295}
296
297static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
299}
300
301void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
302 const char *Modifier, bool NotRIPRel) {
303 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 switch (MO.getType()) {
305 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000306 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 "Virtual registers should not make it this far!");
308 O << '%';
309 unsigned Reg = MO.getReg();
310 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000311 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
313 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
314 Reg = getX86SubSuperRegister(Reg, VT);
315 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000316 O << TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 return;
318 }
319
320 case MachineOperand::MO_Immediate:
321 if (!Modifier ||
322 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
323 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000324 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 return;
326 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000327 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 return;
329 case MachineOperand::MO_JumpTableIndex: {
330 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
331 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000332 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000333 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334
335 if (TM.getRelocationModel() == Reloc::PIC_) {
336 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000337 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
338 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 else if (Subtarget->isPICStyleGOT())
340 O << "@GOTOFF";
341 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
344 O << "(%rip)";
345 return;
346 }
347 case MachineOperand::MO_ConstantPoolIndex: {
348 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
349 if (!isMemOp) O << '$';
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000350 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Chris Lattner6017d482007-12-30 23:10:15 +0000351 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352
353 if (TM.getRelocationModel() == Reloc::PIC_) {
354 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000355 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
356 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 else if (Subtarget->isPICStyleGOT())
358 O << "@GOTOFF";
359 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000360
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 int Offset = MO.getOffset();
362 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000363 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 else if (Offset < 0)
365 O << Offset;
366
367 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
368 O << "(%rip)";
369 return;
370 }
371 case MachineOperand::MO_GlobalAddress: {
372 bool isCallOp = Modifier && !strcmp(Modifier, "call");
373 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
374 bool needCloseParen = false;
375
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000376 const GlobalValue *GV = MO.getGlobal();
377 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
378 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000379 // If GV is an alias then use the aliasee for determining
380 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000381 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
382 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
383 }
384
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 bool isThreadLocal = GVar && GVar->isThreadLocal();
386
387 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000388 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000389
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 if (!isMemOp && !isCallOp)
391 O << '$';
392 else if (Name[0] == '$') {
393 // The name begins with a dollar-sign. In order to avoid having it look
394 // like an integer immediate to the assembler, enclose it in parens.
395 O << '(';
396 needCloseParen = true;
397 }
398
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000399 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 // Link-once, declaration, or Weakly-linked global variables need
401 // non-lazily-resolved stubs
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000402 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 // Dynamically-resolved functions need a stub for the function.
404 if (isCallOp && isa<Function>(GV)) {
405 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000406 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 } else {
408 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000409 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 }
411 } else {
412 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000413 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 O << Name;
415 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000416
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000418 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 } else {
420 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000421 O << "__imp_";
422 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 O << Name;
424
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000425 if (isCallOp) {
426 if (shouldPrintPLT(TM, Subtarget)) {
427 // Assemble call via PLT for externally visible symbols
428 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
429 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 O << "@PLT";
431 }
432 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
433 // Save function name for later type emission
434 FnStubs.insert(Name);
435 }
436 }
437
438 if (GV->hasExternalWeakLinkage())
439 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000440
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 int Offset = MO.getOffset();
442 if (Offset > 0)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000443 O << '+' << Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 else if (Offset < 0)
445 O << Offset;
446
447 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000448 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 O << "@TLSGD"; // general dynamic TLS model
450 else
451 if (GV->isDeclaration())
452 O << "@INDNTPOFF"; // initial exec TLS model
453 else
454 O << "@NTPOFF"; // local exec TLS model
455 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000456 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
458 O << "@GOT";
459 else
460 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000461 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
462 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000463 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 O << "@GOTPCREL";
465
466 if (needCloseParen) {
467 needCloseParen = false;
468 O << ')';
469 }
470
471 // Use rip when possible to reduce code size, except when
472 // index or base register are also part of the address. e.g.
473 // foo(%rip)(%rcx,%rax,4) is not legal
474 O << "(%rip)";
475 }
476 }
477
478 if (needCloseParen)
479 O << ')';
480
481 return;
482 }
483 case MachineOperand::MO_ExternalSymbol: {
484 bool isCallOp = Modifier && !strcmp(Modifier, "call");
485 bool needCloseParen = false;
486 std::string Name(TAI->getGlobalPrefix());
487 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000488 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000490 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 return;
492 }
493 if (!isCallOp)
494 O << '$';
495 else if (Name[0] == '$') {
496 // The name begins with a dollar-sign. In order to avoid having it look
497 // like an integer immediate to the assembler, enclose it in parens.
498 O << '(';
499 needCloseParen = true;
500 }
501
502 O << Name;
503
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000504 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 std::string GOTName(TAI->getGlobalPrefix());
506 GOTName+="_GLOBAL_OFFSET_TABLE_";
507 if (Name == GOTName)
508 // HACK! Emit extra offset to PC during printing GOT offset to
509 // compensate for the size of popl instruction. The resulting code
510 // should look like:
511 // call .piclabel
512 // piclabel:
513 // popl %some_register
514 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
515 O << " + [.-"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000516 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517
518 if (isCallOp)
519 O << "@PLT";
520 }
521
522 if (needCloseParen)
523 O << ')';
524
525 if (!isCallOp && Subtarget->isPICStyleRIPRel())
526 O << "(%rip)";
527
528 return;
529 }
530 default:
531 O << "<unknown operand type>"; return;
532 }
533}
534
535void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000536 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 assert(value <= 7 && "Invalid ssecc argument!");
538 switch (value) {
539 case 0: O << "eq"; break;
540 case 1: O << "lt"; break;
541 case 2: O << "le"; break;
542 case 3: O << "unord"; break;
543 case 4: O << "neq"; break;
544 case 5: O << "nlt"; break;
545 case 6: O << "nle"; break;
546 case 7: O << "ord"; break;
547 }
548}
549
550void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
551 const char *Modifier){
552 assert(isMem(MI, Op) && "Invalid memory reference!");
553 MachineOperand BaseReg = MI->getOperand(Op);
554 MachineOperand IndexReg = MI->getOperand(Op+2);
555 const MachineOperand &DispSpec = MI->getOperand(Op+3);
556
557 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
558 if (DispSpec.isGlobalAddress() ||
559 DispSpec.isConstantPoolIndex() ||
560 DispSpec.isJumpTableIndex()) {
561 printOperand(MI, Op+3, "mem", NotRIPRel);
562 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000563 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
565 O << DispVal;
566 }
567
568 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000569 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000571
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 // There are cases where we can end up with ESP/RSP in the indexreg slot.
573 // If this happens, swap the base/index register to support assemblers that
574 // don't work when the index is *SP.
575 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
576 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
577 std::swap(BaseReg, IndexReg);
578 std::swap(BaseRegOperand, IndexRegOperand);
579 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000580
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000581 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 if (BaseReg.getReg())
583 printOperand(MI, Op+BaseRegOperand, Modifier);
584
585 if (IndexReg.getReg()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000586 O << ',';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 printOperand(MI, Op+IndexRegOperand, Modifier);
588 if (ScaleVal != 1)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000589 O << ',' << ScaleVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000591 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 }
593}
594
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000595void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000596 const MachineBasicBlock *MBB) const {
597 if (!TAI->getSetDirective())
598 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000599
600 // We don't need .set machinery if we have GOT-style relocations
601 if (Subtarget->isPICStyleGOT())
602 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000603
Evan Cheng6fb06762007-11-09 01:32:10 +0000604 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
605 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000606 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000607 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000608 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000609 << '_' << uid << '\n';
610 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000611 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000612}
613
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000615 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000616 O << label << '\n' << label << ':';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617}
618
619
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000620void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
621 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000622 unsigned uid) const
623{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000624 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
625 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
626
627 O << JTEntryDirective << ' ';
628
629 if (TM.getRelocationModel() == Reloc::PIC_) {
630 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
631 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
632 << '_' << uid << "_set_" << MBB->getNumber();
633 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000634 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000635 O << "@GOTOFF";
636 } else
637 assert(0 && "Don't know how to print MBB label for this PIC mode");
638 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000639 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000640}
641
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000642bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 unsigned Reg = MO.getReg();
645 switch (Mode) {
646 default: return true; // Unknown mode.
647 case 'b': // Print QImode register
648 Reg = getX86SubSuperRegister(Reg, MVT::i8);
649 break;
650 case 'h': // Print QImode high register
651 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
652 break;
653 case 'w': // Print HImode register
654 Reg = getX86SubSuperRegister(Reg, MVT::i16);
655 break;
656 case 'k': // Print SImode register
657 Reg = getX86SubSuperRegister(Reg, MVT::i32);
658 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000659 case 'q': // Print DImode register
660 Reg = getX86SubSuperRegister(Reg, MVT::i64);
661 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 }
663
Evan Cheng00d04a72008-07-07 22:21:06 +0000664 O << '%'<< TRI->getAsmName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 return false;
666}
667
668/// PrintAsmOperand - Print out an operand for an inline asm expression.
669///
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000670bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000671 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 const char *ExtraCode) {
673 // Does this asm operand have a single letter operand modifier?
674 if (ExtraCode && ExtraCode[0]) {
675 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000676
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 switch (ExtraCode[0]) {
678 default: return true; // Unknown modifier.
679 case 'c': // Don't print "$" before a global var name or constant.
680 printOperand(MI, OpNo, "mem");
681 return false;
682 case 'b': // Print QImode register
683 case 'h': // Print QImode high register
684 case 'w': // Print HImode register
685 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000686 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000687 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
689 printOperand(MI, OpNo);
690 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 case 'P': // Don't print @PLT, but do print as memory.
693 printOperand(MI, OpNo, "mem");
694 return false;
695 }
696 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000697
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 printOperand(MI, OpNo);
699 return false;
700}
701
Anton Korobeynikov3ab60792008-06-28 11:10:06 +0000702bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000704 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000706 if (ExtraCode && ExtraCode[0]) {
707 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000708
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000709 switch (ExtraCode[0]) {
710 default: return true; // Unknown modifier.
711 case 'b': // Print QImode register
712 case 'h': // Print QImode high register
713 case 'w': // Print HImode register
714 case 'k': // Print SImode register
715 case 'q': // Print SImode register
716 // These only apply to registers, ignore on mem.
717 break;
718 }
719 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 printMemReference(MI, OpNo);
721 return false;
722}
723
724/// printMachineInstruction -- Print out a single X86 LLVM instruction
725/// MI in AT&T syntax to the current output stream.
726///
727void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
728 ++EmittedInsts;
729
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 // Call the autogenerated instruction printer routines.
731 printInstruction(MI);
732}
733
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000734/// doInitialization
735bool X86ATTAsmPrinter::doInitialization(Module &M) {
736 if (TAI->doesSupportDebugInformation()) {
737 // Emit initial debug information.
738 DW.BeginModule(&M);
739 }
740
Evan Cheng5cda7762008-07-09 06:36:53 +0000741 bool Result = AsmPrinter::doInitialization(M);
742
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000743 // Darwin wants symbols to be quoted if they have complex names.
744 if (Subtarget->isTargetDarwin())
745 Mang->setUseQuotes(true);
746
747 return Result;
748}
749
750
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000751void X86ATTAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000752 const TargetData *TD = TM.getTargetData();
753
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000754 if (!GVar->hasInitializer())
755 return; // External global require no code
756
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000757 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikovb81503c2008-07-09 13:24:38 +0000758
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000759 // Check to see if this is a special global used by LLVM, if so, emit it.
760 if (EmitSpecialLLVMGlobal(GVar)) {
761 if (Subtarget->isTargetDarwin() &&
762 TM.getRelocationModel() == Reloc::Static) {
763 if (GVar->getName() == "llvm.global_ctors")
764 O << ".reference .constructors_used\n";
765 else if (GVar->getName() == "llvm.global_dtors")
766 O << ".reference .destructors_used\n";
767 }
768 return;
769 }
770
771 std::string name = Mang->getValueName(GVar);
772 Constant *C = GVar->getInitializer();
773 const Type *Type = C->getType();
774 unsigned Size = TD->getABITypeSize(Type);
775 unsigned Align = TD->getPreferredAlignmentLog(GVar);
776
777 if (GVar->hasHiddenVisibility()) {
778 if (const char *Directive = TAI->getHiddenDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000779 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000780 } else if (GVar->hasProtectedVisibility()) {
781 if (const char *Directive = TAI->getProtectedDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000782 O << Directive << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000783 }
784
785 if (Subtarget->isTargetELF())
786 O << "\t.type\t" << name << ",@object\n";
787
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000788 SwitchToDataSection(SectionName.c_str());
789
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000790 if (C->isNullValue() && !GVar->hasSection()) {
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000791 // FIXME: This seems to be pretty darwin-specific
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000792 if (GVar->hasExternalLinkage()) {
793 if (const char *Directive = TAI->getZeroFillDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000794 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000795 O << Directive << "__DATA, __common, " << name << ", "
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000796 << Size << ", " << Align << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000797 return;
798 }
799 }
800
801 if (!GVar->isThreadLocal() &&
Anton Korobeynikovf6816542008-07-09 13:27:59 +0000802 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000803 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000804
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000805 if (TAI->getLCOMMDirective() != NULL) {
806 if (GVar->hasInternalLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000807 O << TAI->getLCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000808 if (Subtarget->isTargetDarwin())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000809 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000810 } else if (Subtarget->isTargetDarwin() && !GVar->hasCommonLinkage()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000811 O << "\t.globl " << name << '\n'
812 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000813 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000814 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000815 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000816 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000817 EmitGlobalConstant(C);
818 return;
819 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000820 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000821
822 // Leopard and above support aligned common symbols.
823 if (Subtarget->getDarwinVers() >= 9)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000824 O << ',' << Align;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000825 }
826 } else {
827 if (!Subtarget->isTargetCygMing()) {
828 if (GVar->hasInternalLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000829 O << "\t.local\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000830 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000831 O << TAI->getCOMMDirective() << name << ',' << Size;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000832 if (TAI->getCOMMDirectiveTakesAlignment())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000833 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000834 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000835 O << "\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000836 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000837 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000838 return;
839 }
840 }
841
842 switch (GVar->getLinkage()) {
843 case GlobalValue::CommonLinkage:
844 case GlobalValue::LinkOnceLinkage:
845 case GlobalValue::WeakLinkage:
846 if (Subtarget->isTargetDarwin()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000847 O << "\t.globl " << name << '\n'
848 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000849 } else if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000850 O << "\t.globl\t" << name << "\n"
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000851 "\t.linkonce same_size\n";
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000852 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000853 O << "\t.weak\t" << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000854 }
855 break;
856 case GlobalValue::DLLExportLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000857 case GlobalValue::AppendingLinkage:
858 // FIXME: appending linkage variables should go into a section of
859 // their name or something. For now, just emit them as external.
860 case GlobalValue::ExternalLinkage:
861 // If external or appending, declare as a global symbol
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000862 O << "\t.globl " << name << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000863 // FALL THROUGH
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000864 case GlobalValue::InternalLinkage:
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000865 break;
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000866 default:
867 assert(0 && "Unknown linkage type!");
868 }
869
870 EmitAlignment(Align, GVar);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000871 O << name << ":\t\t\t\t" << TAI->getCommentString() << ' ';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000872 PrintUnmangledNameSafely(GVar, O);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000873 O << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000874 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000875 O << "\t.size\t" << name << ", " << Size << '\n';
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000876
877 // If the initializer is a extern weak symbol, remember to emit the weak
878 // reference!
879 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
880 if (GV->hasExternalWeakLinkage())
881 ExtWeakSymbols.insert(GV);
882
883 EmitGlobalConstant(C);
884}
885
Evan Cheng76443dc2008-07-08 00:55:58 +0000886/// printGVStub - Print stub for a global value.
887///
888void X86ATTAsmPrinter::printGVStub(const char *GV, const char *Prefix) {
Evan Cheng1cd2dc52008-07-08 16:40:43 +0000889 printSuffixedName(GV, "$non_lazy_ptr", Prefix);
Evan Cheng76443dc2008-07-08 00:55:58 +0000890 O << ":\n\t.indirect_symbol ";
891 if (Prefix) O << Prefix;
892 O << GV << "\n\t.long\t0\n";
893}
894
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000895
896bool X86ATTAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000897 // Print out module-level global variables here.
898 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000899 I != E; ++I) {
Anton Korobeynikovfc3efd82008-06-28 11:09:32 +0000900 printModuleLevelGV(I);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000901
Anton Korobeynikov0737ff52008-06-28 11:09:48 +0000902 if (I->hasDLLExportLinkage())
903 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
904 }
905
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000906 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000907 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000908 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000909
910 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
911 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000912 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000913 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000914
915 if (!DLLExportedFns.empty()) {
916 SwitchToDataSection(".section .drectve");
917 }
918
919 for (StringSet<>::iterator i = DLLExportedFns.begin(),
920 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000921 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000922 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000923
924 if (Subtarget->isTargetDarwin()) {
925 SwitchToDataSection("");
926
927 // Output stubs for dynamically-linked functions
928 unsigned j = 1;
929 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
930 i != e; ++i, ++j) {
931 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
932 "self_modifying_code+pure_instructions,5", 0);
Evan Cheng76443dc2008-07-08 00:55:58 +0000933 const char *p = i->getKeyData();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000934 printSuffixedName(p, "$stub");
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000935 O << ":\n"
936 "\t.indirect_symbol " << p << "\n"
937 "\thlt ; hlt ; hlt ; hlt ; hlt\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000938 }
939
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000940 O << '\n';
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000941
Evan Cheng76443dc2008-07-08 00:55:58 +0000942 // Print global value stubs.
943 bool InStubSection = false;
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000944 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
945 // Add the (possibly multiple) personalities to the set of global values.
946 // Only referenced functions get into the Personalities list.
947 const std::vector<Function *>& Personalities = MMI->getPersonalities();
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000948 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Evan Cheng76443dc2008-07-08 00:55:58 +0000949 E = Personalities.end(); I != E; ++I) {
950 if (!*I)
951 continue;
952 if (!InStubSection) {
953 SwitchToDataSection(
954 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
955 InStubSection = true;
956 }
957 printGVStub((*I)->getNameStart(), "_");
958 }
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000959 }
960
961 // Output stubs for external and common global variables.
Evan Cheng76443dc2008-07-08 00:55:58 +0000962 if (!InStubSection && !GVStubs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000963 SwitchToDataSection(
964 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
965 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
Evan Cheng76443dc2008-07-08 00:55:58 +0000966 i != e; ++i)
967 printGVStub(i->getKeyData());
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000968
969 // Emit final debug information.
970 DW.EndModule();
971
972 // Funny Darwin hack: This flag tells the linker that no global symbols
973 // contain code that falls through to other global symbols (e.g. the obvious
974 // implementation of multiple entry points). If this doesn't occur, the
975 // linker can safely perform dead code stripping. Since LLVM never
976 // generates code that does this, it is always safe to set.
977 O << "\t.subsections_via_symbols\n";
978 } else if (Subtarget->isTargetCygMing()) {
979 // Emit type information for external functions
980 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
981 i != e; ++i) {
982 O << "\t.def\t " << i->getKeyData()
983 << ";\t.scl\t" << COFF::C_EXT
984 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
985 << ";\t.endef\n";
986 }
987
988 // Emit final debug information.
989 DW.EndModule();
990 } else if (Subtarget->isTargetELF()) {
991 // Emit final debug information.
992 DW.EndModule();
993 }
994
995 return AsmPrinter::doFinalization(M);
996}
997
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998// Include the auto-generated portion of the assembly writer.
999#include "X86GenAsmWriter.inc"