blob: 602fa51e3ae21bc79038819e5b9b32542f8d8c86 [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
35STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
Evan Cheng0729ccf2008-01-05 00:41:47 +000037static std::string getPICLabelString(unsigned FnNum,
38 const TargetAsmInfo *TAI,
39 const X86Subtarget* Subtarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 std::string label;
41 if (Subtarget->isTargetDarwin())
Evan Cheng477013c2007-10-14 05:57:21 +000042 label = "\"L" + utostr_32(FnNum) + "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 else if (Subtarget->isTargetELF())
Evan Cheng0729ccf2008-01-05 00:41:47 +000044 label = ".Lllvm$" + utostr_32(FnNum) + "." + "$piclabel";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 else
46 assert(0 && "Don't know how to print PIC label!\n");
47
48 return label;
49}
50
Anton Korobeynikovab6c6a42008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikovd97b85e2008-06-28 11:08:09 +0000153 case Function::InternalLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +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";
161 } else if (Subtarget->isTargetCygMing()) {
162 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
163 } else {
164 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
165 ",\"ax\",@progbits";
166 }
167 }
168}
169
170/// runOnMachineFunction - This uses the printMachineInstruction()
171/// method to print assembly for each instruction.
172///
173bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
174 if (TAI->doesSupportDebugInformation()) {
175 // Let PassManager know we need debug information and relay
176 // the MachineModuleInfo address on to DwarfWriter.
Bill Wendlingd1bda4f2007-09-11 08:27:17 +0000177 MMI = &getAnalysis<MachineModuleInfo>();
178 DW.SetModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 }
180
181 SetupMachineFunction(MF);
182 O << "\n\n";
183
184 // Print out constants referenced by the function
185 EmitConstantPool(MF.getConstantPool());
186
187 // Print out labels for the function.
188 const Function *F = MF.getFunction();
189 unsigned CC = F->getCallingConv();
190
191 // Populate function information map. Actually, We don't want to populate
192 // non-stdcall or non-fastcall functions' information right now.
193 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
194 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
195
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000196 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
198 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000199
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000200 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 switch (F->getLinkage()) {
202 default: assert(0 && "Unknown linkage type!");
203 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000204 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 break;
206 case Function::DLLExportLinkage:
207 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
208 //FALLS THROUGH
209 case Function::ExternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000210 EmitAlignment(FnAlign, F);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000211 O << "\t.globl\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 break;
213 case Function::LinkOnceLinkage:
214 case Function::WeakLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000215 EmitAlignment(FnAlign, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 if (Subtarget->isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 O << "\t.globl\t" << CurrentFnName << "\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000218 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmanc37918d2007-10-05 15:54:58 +0000220 O << "\t.globl\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 O << "\t.linkonce discard\n";
222 } else {
Dan Gohman721e6582007-07-30 15:08:02 +0000223 O << "\t.weak\t" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 }
225 break;
226 }
227 if (F->hasHiddenVisibility()) {
228 if (const char *Directive = TAI->getHiddenDirective())
229 O << Directive << CurrentFnName << "\n";
230 } else if (F->hasProtectedVisibility()) {
231 if (const char *Directive = TAI->getProtectedDirective())
232 O << Directive << CurrentFnName << "\n";
233 }
234
235 if (Subtarget->isTargetELF())
Dan Gohman721e6582007-07-30 15:08:02 +0000236 O << "\t.type\t" << CurrentFnName << ",@function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 else if (Subtarget->isTargetCygMing()) {
238 O << "\t.def\t " << CurrentFnName
239 << ";\t.scl\t" <<
240 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
241 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
242 << ";\t.endef\n";
243 }
244
245 O << CurrentFnName << ":\n";
246 // Add some workaround for linkonce linkage on Cygwin\MinGW
247 if (Subtarget->isTargetCygMing() &&
248 (F->getLinkage() == Function::LinkOnceLinkage ||
249 F->getLinkage() == Function::WeakLinkage))
250 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
251
Dale Johannesen85535762008-04-02 00:25:04 +0000252 if (TAI->doesSupportDebugInformation() ||
253 TAI->doesSupportExceptionHandling()) {
254 // Emit pre-function debug and/or EH information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 DW.BeginFunction(&MF);
256 }
257
258 // Print out code for the function.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000259 bool hasAnyRealCode = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
261 I != E; ++I) {
262 // Print a label for the basic block.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000263 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000264 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 O << '\n';
266 }
Bill Wendlingb5880a72008-01-26 09:03:52 +0000267 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
268 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // Print the assembly for the instruction.
Dale Johannesenf35771f2008-04-08 00:37:56 +0000270 if (II->getOpcode() != X86::LABEL)
271 hasAnyRealCode = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 printMachineInstruction(II);
273 }
274 }
275
Dale Johannesenf35771f2008-04-08 00:37:56 +0000276 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
277 // If the function is empty, then we need to emit *something*. Otherwise,
278 // the function's label might be associated with something that it wasn't
279 // meant to be associated with. We emit a noop in this situation.
280 // We are assuming inline asms are code.
281 O << "\tnop\n";
282 }
283
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 if (TAI->hasDotTypeDotSizeDirective())
Dan Gohmandd1aa3a2007-08-01 14:42:30 +0000285 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286
287 if (TAI->doesSupportDebugInformation()) {
288 // Emit post-function debug information.
289 DW.EndFunction();
290 }
291
292 // Print out jump tables referenced by the function.
293 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000294
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 // We didn't modify anything.
296 return false;
297}
298
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000299static inline bool shouldPrintGOT(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
301}
302
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000303static inline bool shouldPrintPLT(TargetMachine &TM, const X86Subtarget* ST) {
304 return ST->isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ &&
305 (ST->isPICStyleRIPRel() || ST->isPICStyleGOT());
306}
307
308static inline bool shouldPrintStub(TargetMachine &TM, const X86Subtarget* ST) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
310}
311
312void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
313 const char *Modifier, bool NotRIPRel) {
314 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 switch (MO.getType()) {
316 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000317 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 "Virtual registers should not make it this far!");
319 O << '%';
320 unsigned Reg = MO.getReg();
321 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000322 MVT VT = (strcmp(Modifier+6,"64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
324 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
325 Reg = getX86SubSuperRegister(Reg, VT);
326 }
Evan Cheng3c0eda52008-03-15 00:03:38 +0000327 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 O << (char)tolower(*Name);
329 return;
330 }
331
332 case MachineOperand::MO_Immediate:
333 if (!Modifier ||
334 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
335 O << '$';
Chris Lattnera96056a2007-12-30 20:49:49 +0000336 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 return;
338 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000339 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 return;
341 case MachineOperand::MO_JumpTableIndex: {
342 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
343 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000344 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000345 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346
347 if (TM.getRelocationModel() == Reloc::PIC_) {
348 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000349 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
350 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 else if (Subtarget->isPICStyleGOT())
352 O << "@GOTOFF";
353 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000354
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
356 O << "(%rip)";
357 return;
358 }
359 case MachineOperand::MO_ConstantPoolIndex: {
360 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
361 if (!isMemOp) O << '$';
Evan Cheng477013c2007-10-14 05:57:21 +0000362 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000363 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364
365 if (TM.getRelocationModel() == Reloc::PIC_) {
366 if (Subtarget->isPICStyleStub())
Evan Cheng477013c2007-10-14 05:57:21 +0000367 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
368 << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 else if (Subtarget->isPICStyleGOT())
370 O << "@GOTOFF";
371 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000372
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 int Offset = MO.getOffset();
374 if (Offset > 0)
375 O << "+" << Offset;
376 else if (Offset < 0)
377 O << Offset;
378
379 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
380 O << "(%rip)";
381 return;
382 }
383 case MachineOperand::MO_GlobalAddress: {
384 bool isCallOp = Modifier && !strcmp(Modifier, "call");
385 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
386 bool needCloseParen = false;
387
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000388 const GlobalValue *GV = MO.getGlobal();
389 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
390 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000391 // If GV is an alias then use the aliasee for determining
392 // thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000393 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
394 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
395 }
396
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 bool isThreadLocal = GVar && GVar->isThreadLocal();
398
399 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000400 decorateName(Name, GV);
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 if (!isMemOp && !isCallOp)
403 O << '$';
404 else if (Name[0] == '$') {
405 // The name begins with a dollar-sign. In order to avoid having it look
406 // like an integer immediate to the assembler, enclose it in parens.
407 O << '(';
408 needCloseParen = true;
409 }
410
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000411 if (shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 // Link-once, declaration, or Weakly-linked global variables need
413 // non-lazily-resolved stubs
414 if (GV->isDeclaration() ||
415 GV->hasWeakLinkage() ||
Dale Johannesen49c44122008-05-14 20:12:51 +0000416 GV->hasLinkOnceLinkage() ||
417 GV->hasCommonLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 // Dynamically-resolved functions need a stub for the function.
419 if (isCallOp && isa<Function>(GV)) {
420 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000421 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 } else {
423 GVStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000424 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 }
426 } else {
427 if (GV->hasDLLImportLinkage())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000428 O << "__imp_";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 O << Name;
430 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000431
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng0729ccf2008-01-05 00:41:47 +0000433 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 } else {
435 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000436 O << "__imp_";
437 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 O << Name;
439
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000440 if (isCallOp) {
441 if (shouldPrintPLT(TM, Subtarget)) {
442 // Assemble call via PLT for externally visible symbols
443 if (!GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
444 !GV->hasInternalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 O << "@PLT";
446 }
447 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
448 // Save function name for later type emission
449 FnStubs.insert(Name);
450 }
451 }
452
453 if (GV->hasExternalWeakLinkage())
454 ExtWeakSymbols.insert(GV);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000455
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 int Offset = MO.getOffset();
457 if (Offset > 0)
458 O << "+" << Offset;
459 else if (Offset < 0)
460 O << Offset;
461
462 if (isThreadLocal) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000463 if (TM.getRelocationModel() == Reloc::PIC_ || Subtarget->is64Bit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 O << "@TLSGD"; // general dynamic TLS model
465 else
466 if (GV->isDeclaration())
467 O << "@INDNTPOFF"; // initial exec TLS model
468 else
469 O << "@NTPOFF"; // local exec TLS model
470 } else if (isMemOp) {
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000471 if (shouldPrintGOT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
473 O << "@GOT";
474 else
475 O << "@GOTOFF";
Chris Lattnerfa7ef612007-11-04 19:23:28 +0000476 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel &&
477 TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikov0d38b7d2008-01-20 13:59:37 +0000478 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 O << "@GOTPCREL";
480
481 if (needCloseParen) {
482 needCloseParen = false;
483 O << ')';
484 }
485
486 // Use rip when possible to reduce code size, except when
487 // index or base register are also part of the address. e.g.
488 // foo(%rip)(%rcx,%rax,4) is not legal
489 O << "(%rip)";
490 }
491 }
492
493 if (needCloseParen)
494 O << ')';
495
496 return;
497 }
498 case MachineOperand::MO_ExternalSymbol: {
499 bool isCallOp = Modifier && !strcmp(Modifier, "call");
500 bool needCloseParen = false;
501 std::string Name(TAI->getGlobalPrefix());
502 Name += MO.getSymbolName();
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000503 if (isCallOp && shouldPrintStub(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000505 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 return;
507 }
508 if (!isCallOp)
509 O << '$';
510 else if (Name[0] == '$') {
511 // The name begins with a dollar-sign. In order to avoid having it look
512 // like an integer immediate to the assembler, enclose it in parens.
513 O << '(';
514 needCloseParen = true;
515 }
516
517 O << Name;
518
Rafael Espindolae0ac18d2008-06-09 09:52:31 +0000519 if (shouldPrintPLT(TM, Subtarget)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 std::string GOTName(TAI->getGlobalPrefix());
521 GOTName+="_GLOBAL_OFFSET_TABLE_";
522 if (Name == GOTName)
523 // HACK! Emit extra offset to PC during printing GOT offset to
524 // compensate for the size of popl instruction. The resulting code
525 // should look like:
526 // call .piclabel
527 // piclabel:
528 // popl %some_register
529 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
530 O << " + [.-"
Evan Cheng0729ccf2008-01-05 00:41:47 +0000531 << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532
533 if (isCallOp)
534 O << "@PLT";
535 }
536
537 if (needCloseParen)
538 O << ')';
539
540 if (!isCallOp && Subtarget->isPICStyleRIPRel())
541 O << "(%rip)";
542
543 return;
544 }
545 default:
546 O << "<unknown operand type>"; return;
547 }
548}
549
550void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000551 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 assert(value <= 7 && "Invalid ssecc argument!");
553 switch (value) {
554 case 0: O << "eq"; break;
555 case 1: O << "lt"; break;
556 case 2: O << "le"; break;
557 case 3: O << "unord"; break;
558 case 4: O << "neq"; break;
559 case 5: O << "nlt"; break;
560 case 6: O << "nle"; break;
561 case 7: O << "ord"; break;
562 }
563}
564
565void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
566 const char *Modifier){
567 assert(isMem(MI, Op) && "Invalid memory reference!");
568 MachineOperand BaseReg = MI->getOperand(Op);
569 MachineOperand IndexReg = MI->getOperand(Op+2);
570 const MachineOperand &DispSpec = MI->getOperand(Op+3);
571
572 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
573 if (DispSpec.isGlobalAddress() ||
574 DispSpec.isConstantPoolIndex() ||
575 DispSpec.isJumpTableIndex()) {
576 printOperand(MI, Op+3, "mem", NotRIPRel);
577 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000578 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
580 O << DispVal;
581 }
582
583 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000584 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000586
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 // There are cases where we can end up with ESP/RSP in the indexreg slot.
588 // If this happens, swap the base/index register to support assemblers that
589 // don't work when the index is *SP.
590 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
591 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
592 std::swap(BaseReg, IndexReg);
593 std::swap(BaseRegOperand, IndexRegOperand);
594 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000595
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 O << "(";
597 if (BaseReg.getReg())
598 printOperand(MI, Op+BaseRegOperand, Modifier);
599
600 if (IndexReg.getReg()) {
601 O << ",";
602 printOperand(MI, Op+IndexRegOperand, Modifier);
603 if (ScaleVal != 1)
604 O << "," << ScaleVal;
605 }
606 O << ")";
607 }
608}
609
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000610void X86ATTAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000611 const MachineBasicBlock *MBB) const {
612 if (!TAI->getSetDirective())
613 return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000614
615 // We don't need .set machinery if we have GOT-style relocations
616 if (Subtarget->isPICStyleGOT())
617 return;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000618
Evan Cheng6fb06762007-11-09 01:32:10 +0000619 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
620 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000621 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng5da12252007-11-09 19:11:23 +0000622 if (Subtarget->isPICStyleRIPRel())
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000623 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng5da12252007-11-09 19:11:23 +0000624 << '_' << uid << '\n';
625 else
Evan Cheng0729ccf2008-01-05 00:41:47 +0000626 O << '-' << getPICLabelString(getFunctionNumber(), TAI, Subtarget) << '\n';
Evan Cheng6fb06762007-11-09 01:32:10 +0000627}
628
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng0729ccf2008-01-05 00:41:47 +0000630 std::string label = getPICLabelString(getFunctionNumber(), TAI, Subtarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 O << label << "\n" << label << ":";
632}
633
634
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000635void X86ATTAsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
636 const MachineBasicBlock *MBB,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000637 unsigned uid) const
638{
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000639 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
640 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
641
642 O << JTEntryDirective << ' ';
643
644 if (TM.getRelocationModel() == Reloc::PIC_) {
645 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStub()) {
646 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
647 << '_' << uid << "_set_" << MBB->getNumber();
648 } else if (Subtarget->isPICStyleGOT()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000649 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000650 O << "@GOTOFF";
651 } else
652 assert(0 && "Don't know how to print MBB label for this PIC mode");
653 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000654 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000655}
656
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
658 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 unsigned Reg = MO.getReg();
660 switch (Mode) {
661 default: return true; // Unknown mode.
662 case 'b': // Print QImode register
663 Reg = getX86SubSuperRegister(Reg, MVT::i8);
664 break;
665 case 'h': // Print QImode high register
666 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
667 break;
668 case 'w': // Print HImode register
669 Reg = getX86SubSuperRegister(Reg, MVT::i16);
670 break;
671 case 'k': // Print SImode register
672 Reg = getX86SubSuperRegister(Reg, MVT::i32);
673 break;
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000674 case 'q': // Print DImode register
675 Reg = getX86SubSuperRegister(Reg, MVT::i64);
676 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 }
678
679 O << '%';
Evan Cheng3c0eda52008-03-15 00:03:38 +0000680 for (const char *Name = TRI->getAsmName(Reg); *Name; ++Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 O << (char)tolower(*Name);
682 return false;
683}
684
685/// PrintAsmOperand - Print out an operand for an inline asm expression.
686///
687bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000688 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 const char *ExtraCode) {
690 // Does this asm operand have a single letter operand modifier?
691 if (ExtraCode && ExtraCode[0]) {
692 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000693
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 switch (ExtraCode[0]) {
695 default: return true; // Unknown modifier.
696 case 'c': // Don't print "$" before a global var name or constant.
697 printOperand(MI, OpNo, "mem");
698 return false;
699 case 'b': // Print QImode register
700 case 'h': // Print QImode high register
701 case 'w': // Print HImode register
702 case 'k': // Print SImode register
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000703 case 'q': // Print DImode register
Dan Gohman38a9a9f2007-09-14 20:33:02 +0000704 if (MI->getOperand(OpNo).isRegister())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
706 printOperand(MI, OpNo);
707 return false;
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000708
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 case 'P': // Don't print @PLT, but do print as memory.
710 printOperand(MI, OpNo, "mem");
711 return false;
712 }
713 }
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000714
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 printOperand(MI, OpNo);
716 return false;
717}
718
719bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
720 unsigned OpNo,
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000721 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 const char *ExtraCode) {
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000723 if (ExtraCode && ExtraCode[0]) {
724 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd97b85e2008-06-28 11:08:09 +0000725
Chris Lattner1fabfaa2007-10-29 03:09:07 +0000726 switch (ExtraCode[0]) {
727 default: return true; // Unknown modifier.
728 case 'b': // Print QImode register
729 case 'h': // Print QImode high register
730 case 'w': // Print HImode register
731 case 'k': // Print SImode register
732 case 'q': // Print SImode register
733 // These only apply to registers, ignore on mem.
734 break;
735 }
736 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 printMemReference(MI, OpNo);
738 return false;
739}
740
741/// printMachineInstruction -- Print out a single X86 LLVM instruction
742/// MI in AT&T syntax to the current output stream.
743///
744void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
745 ++EmittedInsts;
746
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 // Call the autogenerated instruction printer routines.
748 printInstruction(MI);
749}
750
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000751/// doInitialization
752bool X86ATTAsmPrinter::doInitialization(Module &M) {
753 if (TAI->doesSupportDebugInformation()) {
754 // Emit initial debug information.
755 DW.BeginModule(&M);
756 }
757
758 bool Result = AsmPrinter::doInitialization(M);
759
760 // Darwin wants symbols to be quoted if they have complex names.
761 if (Subtarget->isTargetDarwin())
762 Mang->setUseQuotes(true);
763
764 return Result;
765}
766
767
768bool X86ATTAsmPrinter::doFinalization(Module &M) {
769 // Note: this code is not shared by the Intel printer as it is too different
770 // from how MASM does things. When making changes here don't forget to look
771 // at X86IntelAsmPrinter::doFinalization().
772 const TargetData *TD = TM.getTargetData();
773
774 // Print out module-level global variables here.
775 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
776 I != E; ++I) {
777 if (!I->hasInitializer())
778 continue; // External global require no code
779
780 // Check to see if this is a special global used by LLVM, if so, emit it.
781 if (EmitSpecialLLVMGlobal(I)) {
782 if (Subtarget->isTargetDarwin() &&
783 TM.getRelocationModel() == Reloc::Static) {
784 if (I->getName() == "llvm.global_ctors")
785 O << ".reference .constructors_used\n";
786 else if (I->getName() == "llvm.global_dtors")
787 O << ".reference .destructors_used\n";
788 }
789 continue;
790 }
791
792 std::string name = Mang->getValueName(I);
793 Constant *C = I->getInitializer();
794 const Type *Type = C->getType();
795 unsigned Size = TD->getABITypeSize(Type);
796 unsigned Align = TD->getPreferredAlignmentLog(I);
797
798 if (I->hasHiddenVisibility()) {
799 if (const char *Directive = TAI->getHiddenDirective())
800 O << Directive << name << "\n";
801 } else if (I->hasProtectedVisibility()) {
802 if (const char *Directive = TAI->getProtectedDirective())
803 O << Directive << name << "\n";
804 }
805
806 if (Subtarget->isTargetELF())
807 O << "\t.type\t" << name << ",@object\n";
808
809 if (C->isNullValue() && !I->hasSection()) {
810 if (I->hasExternalLinkage()) {
811 if (const char *Directive = TAI->getZeroFillDirective()) {
812 O << "\t.globl " << name << "\n";
813 O << Directive << "__DATA, __common, " << name << ", "
814 << Size << ", " << Align << "\n";
815 continue;
816 }
817 }
818
819 if (!I->isThreadLocal() &&
820 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
821 I->hasLinkOnceLinkage() || I->hasCommonLinkage())) {
822 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
823 if (!NoZerosInBSS && TAI->getBSSSection())
824 SwitchToDataSection(TAI->getBSSSection(), I);
825 else
826 SwitchToDataSection(TAI->getDataSection(), I);
827 if (TAI->getLCOMMDirective() != NULL) {
828 if (I->hasInternalLinkage()) {
829 O << TAI->getLCOMMDirective() << name << "," << Size;
830 if (Subtarget->isTargetDarwin())
831 O << "," << Align;
832 } else if (Subtarget->isTargetDarwin() && !I->hasCommonLinkage()) {
833 O << "\t.globl " << name << "\n"
834 << TAI->getWeakDefDirective() << name << "\n";
835 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
836 EmitAlignment(Align, I);
837 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
838 PrintUnmangledNameSafely(I, O);
839 O << "\n";
840 EmitGlobalConstant(C);
841 continue;
842 } else {
843 O << TAI->getCOMMDirective() << name << "," << Size;
844
845 // Leopard and above support aligned common symbols.
846 if (Subtarget->getDarwinVers() >= 9)
847 O << "," << Align;
848 }
849 } else {
850 if (!Subtarget->isTargetCygMing()) {
851 if (I->hasInternalLinkage())
852 O << "\t.local\t" << name << "\n";
853 }
854 O << TAI->getCOMMDirective() << name << "," << Size;
855 if (TAI->getCOMMDirectiveTakesAlignment())
856 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
857 }
858 O << "\t\t" << TAI->getCommentString() << " ";
859 PrintUnmangledNameSafely(I, O);
860 O << "\n";
861 continue;
862 }
863 }
864
865 switch (I->getLinkage()) {
866 case GlobalValue::CommonLinkage:
867 case GlobalValue::LinkOnceLinkage:
868 case GlobalValue::WeakLinkage:
869 if (Subtarget->isTargetDarwin()) {
870 O << "\t.globl " << name << "\n"
871 << TAI->getWeakDefDirective() << name << "\n";
872 if (!I->isConstant())
873 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
874 else {
875 const ArrayType *AT = dyn_cast<ArrayType>(Type);
876 if (AT && AT->getElementType()==Type::Int8Ty)
877 SwitchToDataSection("\t.section __TEXT,__const_coal,coalesced", I);
878 else
879 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
880 }
881 } else if (Subtarget->isTargetCygMing()) {
882 std::string SectionName(".section\t.data$linkonce." +
883 name +
884 ",\"aw\"");
885 SwitchToDataSection(SectionName.c_str(), I);
886 O << "\t.globl\t" << name << "\n"
887 << "\t.linkonce same_size\n";
888 } else {
889 std::string SectionName("\t.section\t.llvm.linkonce.d." +
890 name +
891 ",\"aw\",@progbits");
892 SwitchToDataSection(SectionName.c_str(), I);
893 O << "\t.weak\t" << name << "\n";
894 }
895 break;
896 case GlobalValue::DLLExportLinkage:
897 DLLExportedGVs.insert(Mang->makeNameProper(I->getName(),""));
898 // FALL THROUGH
899 case GlobalValue::AppendingLinkage:
900 // FIXME: appending linkage variables should go into a section of
901 // their name or something. For now, just emit them as external.
902 case GlobalValue::ExternalLinkage:
903 // If external or appending, declare as a global symbol
904 O << "\t.globl " << name << "\n";
905 // FALL THROUGH
906 case GlobalValue::InternalLinkage: {
907 if (I->isConstant()) {
908 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
909 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
910 SwitchToDataSection(TAI->getCStringSection(), I);
911 break;
912 }
913 }
914 // FIXME: special handling for ".ctors" & ".dtors" sections
915 if (I->hasSection() &&
916 (I->getSection() == ".ctors" ||
917 I->getSection() == ".dtors")) {
918 std::string SectionName = ".section " + I->getSection();
919
920 if (Subtarget->isTargetCygMing()) {
921 SectionName += ",\"aw\"";
922 } else {
923 assert(!Subtarget->isTargetDarwin());
924 SectionName += ",\"aw\",@progbits";
925 }
926 SwitchToDataSection(SectionName.c_str());
927 } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
928 // Honor all section names on Darwin; ObjC uses this
929 std::string SectionName = ".section " + I->getSection();
930 SwitchToDataSection(SectionName.c_str());
931 } else {
932 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
933 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
934 TAI->getBSSSection(), I);
935 else if (!I->isConstant())
936 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
937 TAI->getDataSection(), I);
938 else if (I->isThreadLocal())
939 SwitchToDataSection(TAI->getTLSDataSection());
940 else {
941 // Read-only data.
942 bool HasReloc = C->ContainsRelocations();
943 if (HasReloc &&
944 Subtarget->isTargetDarwin() &&
945 TM.getRelocationModel() != Reloc::Static)
946 SwitchToDataSection("\t.const_data\n");
947 else if (!HasReloc && Size == 4 &&
948 TAI->getFourByteConstantSection())
949 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
950 else if (!HasReloc && Size == 8 &&
951 TAI->getEightByteConstantSection())
952 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
953 else if (!HasReloc && Size == 16 &&
954 TAI->getSixteenByteConstantSection())
955 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
956 else if (TAI->getReadOnlySection())
957 SwitchToDataSection(TAI->getReadOnlySection(), I);
958 else
959 SwitchToDataSection(TAI->getDataSection(), I);
960 }
961 }
962
963 break;
964 }
965 default:
966 assert(0 && "Unknown linkage type!");
967 }
968
969 EmitAlignment(Align, I);
970 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
971 PrintUnmangledNameSafely(I, O);
972 O << "\n";
973 if (TAI->hasDotTypeDotSizeDirective())
974 O << "\t.size\t" << name << ", " << Size << "\n";
975 // If the initializer is a extern weak symbol, remember to emit the weak
976 // reference!
977 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
978 if (GV->hasExternalWeakLinkage())
979 ExtWeakSymbols.insert(GV);
980
981 EmitGlobalConstant(C);
982 }
983
984 // Output linker support code for dllexported globals
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000985 if (!DLLExportedGVs.empty())
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000986 SwitchToDataSection(".section .drectve");
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000987
988 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
989 e = DLLExportedGVs.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000990 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000991 O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +0000992
993 if (!DLLExportedFns.empty()) {
994 SwitchToDataSection(".section .drectve");
995 }
996
997 for (StringSet<>::iterator i = DLLExportedFns.begin(),
998 e = DLLExportedFns.end();
Anton Korobeynikov06ac62e2008-06-28 11:08:44 +0000999 i != e; ++i)
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001000 O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
Anton Korobeynikovab6c6a42008-06-28 11:08:27 +00001001
1002 if (Subtarget->isTargetDarwin()) {
1003 SwitchToDataSection("");
1004
1005 // Output stubs for dynamically-linked functions
1006 unsigned j = 1;
1007 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1008 i != e; ++i, ++j) {
1009 SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
1010 "self_modifying_code+pure_instructions,5", 0);
1011 std::string p = i->getKeyData();
1012 printSuffixedName(p, "$stub");
1013 O << ":\n";
1014 O << "\t.indirect_symbol " << p << "\n";
1015 O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
1016 }
1017
1018 O << "\n";
1019
1020 if (TAI->doesSupportExceptionHandling() && MMI && !Subtarget->is64Bit()) {
1021 // Add the (possibly multiple) personalities to the set of global values.
1022 // Only referenced functions get into the Personalities list.
1023 const std::vector<Function *>& Personalities = MMI->getPersonalities();
1024
1025 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
1026 E = Personalities.end(); I != E; ++I)
1027 if (*I) GVStubs.insert("_" + (*I)->getName());
1028 }
1029
1030 // Output stubs for external and common global variables.
1031 if (!GVStubs.empty())
1032 SwitchToDataSection(
1033 "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
1034 for (StringSet<>::iterator i = GVStubs.begin(), e = GVStubs.end();
1035 i != e; ++i) {
1036 std::string p = i->getKeyData();
1037 printSuffixedName(p, "$non_lazy_ptr");
1038 O << ":\n";
1039 O << "\t.indirect_symbol " << p << "\n";
1040 O << "\t.long\t0\n";
1041 }
1042
1043 // Emit final debug information.
1044 DW.EndModule();
1045
1046 // Funny Darwin hack: This flag tells the linker that no global symbols
1047 // contain code that falls through to other global symbols (e.g. the obvious
1048 // implementation of multiple entry points). If this doesn't occur, the
1049 // linker can safely perform dead code stripping. Since LLVM never
1050 // generates code that does this, it is always safe to set.
1051 O << "\t.subsections_via_symbols\n";
1052 } else if (Subtarget->isTargetCygMing()) {
1053 // Emit type information for external functions
1054 for (StringSet<>::iterator i = FnStubs.begin(), e = FnStubs.end();
1055 i != e; ++i) {
1056 O << "\t.def\t " << i->getKeyData()
1057 << ";\t.scl\t" << COFF::C_EXT
1058 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
1059 << ";\t.endef\n";
1060 }
1061
1062 // Emit final debug information.
1063 DW.EndModule();
1064 } else if (Subtarget->isTargetELF()) {
1065 // Emit final debug information.
1066 DW.EndModule();
1067 }
1068
1069 return AsmPrinter::doFinalization(M);
1070}
1071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072// Include the auto-generated portion of the assembly writer.
1073#include "X86GenAsmWriter.inc"