blob: 85ff09a3be546bc9f267a5bbf9397b05a8bde07a [file] [log] [blame]
Chris Lattner0dc32ea2009-09-20 07:41:30 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner0dc32ea2009-09-20 07:41:30 +000010// 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'.
Chris Lattner72614082002-10-25 22:55:53 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner0dc32ea2009-09-20 07:41:30 +000016#define DEBUG_TYPE "asm-printer"
17#include "X86AsmPrinter.h"
18#include "X86ATTInstPrinter.h"
19#include "X86IntelInstPrinter.h"
20#include "X86MCInstLower.h"
21#include "X86.h"
22#include "X86COFF.h"
23#include "X86COFFMachineModuleInfo.h"
24#include "X86MachineFunctionInfo.h"
25#include "X86TargetMachine.h"
26#include "llvm/CallingConv.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Module.h"
29#include "llvm/Type.h"
30#include "llvm/Assembly/Writer.h"
31#include "llvm/MC/MCContext.h"
32#include "llvm/MC/MCSectionMachO.h"
33#include "llvm/MC/MCStreamer.h"
34#include "llvm/MC/MCSymbol.h"
35#include "llvm/CodeGen/MachineJumpTableInfo.h"
36#include "llvm/CodeGen/MachineModuleInfoImpls.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/FormattedStream.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000039#include "llvm/MC/MCAsmInfo.h"
40#include "llvm/Target/TargetLoweringObjectFile.h"
41#include "llvm/Target/TargetOptions.h"
42#include "llvm/Target/TargetRegistry.h"
43#include "llvm/ADT/SmallString.h"
44#include "llvm/ADT/Statistic.h"
45using namespace llvm;
46
47STATISTIC(EmittedInsts, "Number of machine instrs printed");
48
49//===----------------------------------------------------------------------===//
50// Primitive Helper Functions.
51//===----------------------------------------------------------------------===//
52
53void X86AsmPrinter::printMCInst(const MCInst *MI) {
54 if (MAI->getAssemblerDialect() == 0)
55 X86ATTInstPrinter(O, *MAI).printInstruction(MI);
56 else
57 X86IntelInstPrinter(O, *MAI).printInstruction(MI);
58}
59
60void X86AsmPrinter::PrintPICBaseSymbol() const {
61 // FIXME: Gross const cast hack.
62 X86AsmPrinter *AP = const_cast<X86AsmPrinter*>(this);
Chris Lattner10b318b2010-01-17 21:43:43 +000063 O << *X86MCInstLower(OutContext, 0, *AP).GetPICBaseSymbol();
Chris Lattner0dc32ea2009-09-20 07:41:30 +000064}
65
66void X86AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
67 unsigned FnAlign = MF.getAlignment();
68 const Function *F = MF.getFunction();
69
70 if (Subtarget->isTargetCygMing()) {
71 X86COFFMachineModuleInfo &COFFMMI =
72 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattner4fb69f42010-01-16 00:51:39 +000073 COFFMMI.DecorateCygMingName(CurrentFnSym, OutContext, F,
74 *TM.getTargetData());
Chris Lattner0dc32ea2009-09-20 07:41:30 +000075 }
76
77 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
78 EmitAlignment(FnAlign, F);
79
80 switch (F->getLinkage()) {
81 default: llvm_unreachable("Unknown linkage type!");
82 case Function::InternalLinkage: // Symbols default to internal.
83 case Function::PrivateLinkage:
84 break;
85 case Function::DLLExportLinkage:
86 case Function::ExternalLinkage:
Chris Lattnera5ad93a2010-01-23 06:39:22 +000087 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_Global);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000088 break;
89 case Function::LinkerPrivateLinkage:
90 case Function::LinkOnceAnyLinkage:
91 case Function::LinkOnceODRLinkage:
92 case Function::WeakAnyLinkage:
93 case Function::WeakODRLinkage:
94 if (Subtarget->isTargetDarwin()) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +000095 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_Global);
Chris Lattner10b318b2010-01-17 21:43:43 +000096 O << MAI->getWeakDefDirective() << *CurrentFnSym << '\n';
Chris Lattner0dc32ea2009-09-20 07:41:30 +000097 } else if (Subtarget->isTargetCygMing()) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +000098 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_Global);
Chris Lattner5957c842010-01-18 00:59:24 +000099 O << "\t.linkonce discard\n";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000100 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000101 O << "\t.weak\t" << *CurrentFnSym << '\n';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000102 }
103 break;
104 }
105
Chris Lattner551cdd52010-01-16 00:32:38 +0000106 printVisibility(CurrentFnSym, F->getVisibility());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000107
Chris Lattner551cdd52010-01-16 00:32:38 +0000108 if (Subtarget->isTargetELF()) {
Chris Lattnera800f7c2010-01-25 18:33:40 +0000109 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
Chris Lattner551cdd52010-01-16 00:32:38 +0000110 } else if (Subtarget->isTargetCygMing()) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000111 O << "\t.def\t " << *CurrentFnSym;
Chris Lattner551cdd52010-01-16 00:32:38 +0000112 O << ";\t.scl\t" <<
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000113 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
114 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
115 << ";\t.endef\n";
116 }
117
Chris Lattner10b318b2010-01-17 21:43:43 +0000118 O << *CurrentFnSym << ':';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000119 if (VerboseAsm) {
120 O.PadToColumn(MAI->getCommentColumn());
121 O << MAI->getCommentString() << ' ';
122 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
123 }
124 O << '\n';
125
126 // Add some workaround for linkonce linkage on Cygwin\MinGW
127 if (Subtarget->isTargetCygMing() &&
Chris Lattner10b318b2010-01-17 21:43:43 +0000128 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
129 O << "Lllvm$workaround$fake$stub$" << *CurrentFnSym << ":\n";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000130}
131
132/// runOnMachineFunction - This uses the printMachineInstruction()
133/// method to print assembly for each instruction.
134///
135bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
136 const Function *F = MF.getFunction();
137 this->MF = &MF;
138 CallingConv::ID CC = F->getCallingConv();
139
140 SetupMachineFunction(MF);
141 O << "\n\n";
142
143 if (Subtarget->isTargetCOFF()) {
144 X86COFFMachineModuleInfo &COFFMMI =
145 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
146
147 // Populate function information map. Don't want to populate
148 // non-stdcall or non-fastcall functions' information right now.
149 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
150 COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>());
151 }
152
153 // Print out constants referenced by the function
154 EmitConstantPool(MF.getConstantPool());
155
156 // Print the 'header' of function
157 emitFunctionHeader(MF);
158
159 // Emit pre-function debug and/or EH information.
160 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
161 DW->BeginFunction(&MF);
162
163 // Print out code for the function.
164 bool hasAnyRealCode = false;
165 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
166 I != E; ++I) {
167 // Print a label for the basic block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +0000168 EmitBasicBlockStart(I);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000169 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
170 II != IE; ++II) {
171 // Print the assembly for the instruction.
172 if (!II->isLabel())
173 hasAnyRealCode = true;
174 printMachineInstruction(II);
175 }
176 }
177
178 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
179 // If the function is empty, then we need to emit *something*. Otherwise,
180 // the function's label might be associated with something that it wasn't
181 // meant to be associated with. We emit a noop in this situation.
182 // We are assuming inline asms are code.
183 O << "\tnop\n";
184 }
185
Chris Lattner10b318b2010-01-17 21:43:43 +0000186 if (MAI->hasDotTypeDotSizeDirective())
187 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000188
189 // Emit post-function debug information.
190 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
191 DW->EndFunction(&MF);
192
193 // Print out jump tables referenced by the function.
194 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
195
196 // We didn't modify anything.
197 return false;
198}
199
200/// printSymbolOperand - Print a raw symbol reference operand. This handles
201/// jump tables, constant pools, global address and external symbols, all of
202/// which print to a label with various suffixes for relocation types etc.
203void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) {
204 switch (MO.getType()) {
205 default: llvm_unreachable("unknown symbol type!");
206 case MachineOperand::MO_JumpTableIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +0000207 O << *GetJTISymbol(MO.getIndex());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000208 break;
209 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +0000210 O << *GetCPISymbol(MO.getIndex());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000211 printOffset(MO.getOffset());
212 break;
213 case MachineOperand::MO_GlobalAddress: {
214 const GlobalValue *GV = MO.getGlobal();
215
Chris Lattner5957c842010-01-18 00:59:24 +0000216 MCSymbol *GVSym;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000217 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattner7a2ba942010-01-16 18:37:32 +0000218 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000219 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
220 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
221 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattner7a2ba942010-01-16 18:37:32 +0000222 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner4fb69f42010-01-16 00:51:39 +0000223 else
224 GVSym = GetGlobalValueSymbol(GV);
225
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000226 if (Subtarget->isTargetCygMing()) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000227 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000228 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattner4fb69f42010-01-16 00:51:39 +0000229 COFFMMI.DecorateCygMingName(GVSym, OutContext, GV, *TM.getTargetData());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000230 }
231
232 // Handle dllimport linkage.
233 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner4fb69f42010-01-16 00:51:39 +0000234 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000235
236 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
237 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000238 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000239
240 const MCSymbol *&StubSym =
241 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Chris Lattner8570f592010-01-16 01:00:27 +0000242 if (StubSym == 0)
243 StubSym = GetGlobalValueSymbol(GV);
244
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000245 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattner7a2ba942010-01-16 18:37:32 +0000246 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000247 const MCSymbol *&StubSym =
248 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Chris Lattner8570f592010-01-16 01:00:27 +0000249 if (StubSym == 0)
250 StubSym = GetGlobalValueSymbol(GV);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000251 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000252 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000253 const MCSymbol *&StubSym =
254 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Chris Lattner8570f592010-01-16 01:00:27 +0000255 if (StubSym == 0)
256 StubSym = GetGlobalValueSymbol(GV);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000257 }
258
259 // If the name begins with a dollar-sign, enclose it in parens. We do this
260 // to avoid having it look like an integer immediate to the assembler.
Chris Lattner4fb69f42010-01-16 00:51:39 +0000261 if (GVSym->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000262 O << *GVSym;
263 else
264 O << '(' << *GVSym << ')';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000265 printOffset(MO.getOffset());
266 break;
267 }
268 case MachineOperand::MO_ExternalSymbol: {
Chris Lattneree9250b2010-01-13 07:56:59 +0000269 const MCSymbol *SymToPrint;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000270 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner8570f592010-01-16 01:00:27 +0000271 SmallString<128> TempNameStr;
272 TempNameStr += StringRef(MO.getSymbolName());
273 TempNameStr += StringRef("$stub");
274
275 const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000276 const MCSymbol *&StubSym =
277 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
278 if (StubSym == 0) {
Chris Lattner48130352010-01-13 06:38:18 +0000279 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
280 StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000281 }
Chris Lattneree9250b2010-01-13 07:56:59 +0000282 SymToPrint = StubSym;
283 } else {
Chris Lattner8570f592010-01-16 01:00:27 +0000284 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000285 }
286
287 // If the name begins with a dollar-sign, enclose it in parens. We do this
288 // to avoid having it look like an integer immediate to the assembler.
Chris Lattneree9250b2010-01-13 07:56:59 +0000289 if (SymToPrint->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000290 O << *SymToPrint;
291 else
292 O << '(' << *SymToPrint << '(';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000293 break;
294 }
295 }
296
297 switch (MO.getTargetFlags()) {
298 default:
299 llvm_unreachable("Unknown target flag on GV operand");
300 case X86II::MO_NO_FLAG: // No flag.
301 break;
302 case X86II::MO_DARWIN_NONLAZY:
303 case X86II::MO_DLLIMPORT:
304 case X86II::MO_DARWIN_STUB:
305 // These affect the name of the symbol, not any suffix.
306 break;
307 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
308 O << " + [.-";
309 PrintPICBaseSymbol();
310 O << ']';
311 break;
312 case X86II::MO_PIC_BASE_OFFSET:
313 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
314 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
315 O << '-';
316 PrintPICBaseSymbol();
317 break;
318 case X86II::MO_TLSGD: O << "@TLSGD"; break;
319 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
320 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
321 case X86II::MO_TPOFF: O << "@TPOFF"; break;
322 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
323 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
324 case X86II::MO_GOT: O << "@GOT"; break;
325 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
326 case X86II::MO_PLT: O << "@PLT"; break;
327 }
328}
329
330/// print_pcrel_imm - This is used to print an immediate value that ends up
331/// being encoded as a pc-relative value. These print slightly differently, for
332/// example, a $ is not emitted.
333void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
334 const MachineOperand &MO = MI->getOperand(OpNo);
335 switch (MO.getType()) {
336 default: llvm_unreachable("Unknown pcrel immediate operand");
337 case MachineOperand::MO_Immediate:
338 O << MO.getImm();
339 return;
340 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner10b318b2010-01-17 21:43:43 +0000341 O << *GetMBBSymbol(MO.getMBB()->getNumber());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000342 return;
343 case MachineOperand::MO_GlobalAddress:
344 case MachineOperand::MO_ExternalSymbol:
345 printSymbolOperand(MO);
346 return;
347 }
348}
349
350
351void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner5957c842010-01-18 00:59:24 +0000352 const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000353 const MachineOperand &MO = MI->getOperand(OpNo);
354 switch (MO.getType()) {
355 default: llvm_unreachable("unknown operand type!");
356 case MachineOperand::MO_Register: {
357 O << '%';
358 unsigned Reg = MO.getReg();
359 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
360 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
361 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
362 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
363 Reg = getX86SubSuperRegister(Reg, VT);
364 }
365 O << X86ATTInstPrinter::getRegisterName(Reg);
366 return;
367 }
368
369 case MachineOperand::MO_Immediate:
370 O << '$' << MO.getImm();
371 return;
372
373 case MachineOperand::MO_JumpTableIndex:
374 case MachineOperand::MO_ConstantPoolIndex:
375 case MachineOperand::MO_GlobalAddress:
376 case MachineOperand::MO_ExternalSymbol: {
377 O << '$';
378 printSymbolOperand(MO);
379 break;
380 }
381 }
382}
383
384void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
385 unsigned char value = MI->getOperand(Op).getImm();
386 assert(value <= 7 && "Invalid ssecc argument!");
387 switch (value) {
388 case 0: O << "eq"; break;
389 case 1: O << "lt"; break;
390 case 2: O << "le"; break;
391 case 3: O << "unord"; break;
392 case 4: O << "neq"; break;
393 case 5: O << "nlt"; break;
394 case 6: O << "nle"; break;
395 case 7: O << "ord"; break;
396 }
397}
398
399void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
400 const char *Modifier) {
401 const MachineOperand &BaseReg = MI->getOperand(Op);
402 const MachineOperand &IndexReg = MI->getOperand(Op+2);
403 const MachineOperand &DispSpec = MI->getOperand(Op+3);
404
405 // If we really don't want to print out (rip), don't.
406 bool HasBaseReg = BaseReg.getReg() != 0;
407 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
408 BaseReg.getReg() == X86::RIP)
409 HasBaseReg = false;
410
411 // HasParenPart - True if we will print out the () part of the mem ref.
412 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
413
414 if (DispSpec.isImm()) {
415 int DispVal = DispSpec.getImm();
416 if (DispVal || !HasParenPart)
417 O << DispVal;
418 } else {
419 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
420 DispSpec.isJTI() || DispSpec.isSymbol());
421 printSymbolOperand(MI->getOperand(Op+3));
422 }
423
424 if (HasParenPart) {
425 assert(IndexReg.getReg() != X86::ESP &&
426 "X86 doesn't allow scaling by ESP");
427
428 O << '(';
429 if (HasBaseReg)
430 printOperand(MI, Op, Modifier);
431
432 if (IndexReg.getReg()) {
433 O << ',';
434 printOperand(MI, Op+2, Modifier);
435 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
436 if (ScaleVal != 1)
437 O << ',' << ScaleVal;
438 }
439 O << ')';
440 }
441}
442
443void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
444 const char *Modifier) {
445 assert(isMem(MI, Op) && "Invalid memory reference!");
446 const MachineOperand &Segment = MI->getOperand(Op+4);
447 if (Segment.getReg()) {
448 printOperand(MI, Op+4, Modifier);
449 O << ':';
450 }
451 printLeaMemReference(MI, Op, Modifier);
452}
453
454void X86AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
455 const MachineBasicBlock *MBB) const {
456 if (!MAI->getSetDirective())
457 return;
458
459 // We don't need .set machinery if we have GOT-style relocations
460 if (Subtarget->isPICStyleGOT())
461 return;
462
463 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
464 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
465
Chris Lattner950558a2010-01-18 00:21:06 +0000466 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000467
468 if (Subtarget->isPICStyleRIPRel())
Chris Lattner64c2b242010-01-23 05:26:25 +0000469 O << '-' << *GetJTISymbol(uid) << '\n';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000470 else {
471 O << '-';
472 PrintPICBaseSymbol();
473 O << '\n';
474 }
475}
476
477
478void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
479 PrintPICBaseSymbol();
480 O << '\n';
481 PrintPICBaseSymbol();
482 O << ':';
483}
484
485void X86AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
486 const MachineBasicBlock *MBB,
487 unsigned uid) const {
488 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
489 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
490
491 O << JTEntryDirective << ' ';
492
493 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
494 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
495 << '_' << uid << "_set_" << MBB->getNumber();
Chris Lattner10b318b2010-01-17 21:43:43 +0000496 } else if (Subtarget->isPICStyleGOT())
497 O << *GetMBBSymbol(MBB->getNumber()) << "@GOTOFF";
498 else
499 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000500}
501
502bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
503 unsigned Reg = MO.getReg();
504 switch (Mode) {
505 default: return true; // Unknown mode.
506 case 'b': // Print QImode register
507 Reg = getX86SubSuperRegister(Reg, MVT::i8);
508 break;
509 case 'h': // Print QImode high register
510 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
511 break;
512 case 'w': // Print HImode register
513 Reg = getX86SubSuperRegister(Reg, MVT::i16);
514 break;
515 case 'k': // Print SImode register
516 Reg = getX86SubSuperRegister(Reg, MVT::i32);
517 break;
518 case 'q': // Print DImode register
519 Reg = getX86SubSuperRegister(Reg, MVT::i64);
520 break;
521 }
522
523 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
524 return false;
525}
526
527/// PrintAsmOperand - Print out an operand for an inline asm expression.
528///
529bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
530 unsigned AsmVariant,
531 const char *ExtraCode) {
532 // Does this asm operand have a single letter operand modifier?
533 if (ExtraCode && ExtraCode[0]) {
534 if (ExtraCode[1] != 0) return true; // Unknown modifier.
535
536 const MachineOperand &MO = MI->getOperand(OpNo);
537
538 switch (ExtraCode[0]) {
539 default: return true; // Unknown modifier.
540 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
541 if (MO.isImm()) {
542 O << MO.getImm();
543 return false;
544 }
545 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
546 printSymbolOperand(MO);
547 return false;
548 }
549 if (MO.isReg()) {
550 O << '(';
551 printOperand(MI, OpNo);
552 O << ')';
553 return false;
554 }
555 return true;
556
557 case 'c': // Don't print "$" before a global var name or constant.
558 if (MO.isImm())
559 O << MO.getImm();
560 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
561 printSymbolOperand(MO);
562 else
563 printOperand(MI, OpNo);
564 return false;
565
566 case 'A': // Print '*' before a register (it must be a register)
567 if (MO.isReg()) {
568 O << '*';
569 printOperand(MI, OpNo);
570 return false;
571 }
572 return true;
573
574 case 'b': // Print QImode register
575 case 'h': // Print QImode high register
576 case 'w': // Print HImode register
577 case 'k': // Print SImode register
578 case 'q': // Print DImode register
579 if (MO.isReg())
580 return printAsmMRegister(MO, ExtraCode[0]);
581 printOperand(MI, OpNo);
582 return false;
583
584 case 'P': // This is the operand of a call, treat specially.
585 print_pcrel_imm(MI, OpNo);
586 return false;
587
588 case 'n': // Negate the immediate or print a '-' before the operand.
589 // Note: this is a temporary solution. It should be handled target
590 // independently as part of the 'MC' work.
591 if (MO.isImm()) {
592 O << -MO.getImm();
593 return false;
594 }
595 O << '-';
596 }
597 }
598
599 printOperand(MI, OpNo);
600 return false;
601}
602
603bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
604 unsigned OpNo, unsigned AsmVariant,
605 const char *ExtraCode) {
606 if (ExtraCode && ExtraCode[0]) {
607 if (ExtraCode[1] != 0) return true; // Unknown modifier.
608
609 switch (ExtraCode[0]) {
610 default: return true; // Unknown modifier.
611 case 'b': // Print QImode register
612 case 'h': // Print QImode high register
613 case 'w': // Print HImode register
614 case 'k': // Print SImode register
615 case 'q': // Print SImode register
616 // These only apply to registers, ignore on mem.
617 break;
618 case 'P': // Don't print @PLT, but do print as memory.
619 printMemReference(MI, OpNo, "no-rip");
620 return false;
621 }
622 }
623 printMemReference(MI, OpNo);
624 return false;
625}
626
627
628
629/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
630/// AT&T syntax to the current output stream.
631///
632void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
633 ++EmittedInsts;
634
Devang Patelaf0e2722009-10-06 02:19:11 +0000635 processDebugLoc(MI, true);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000636
637 printInstructionThroughMCStreamer(MI);
638
David Greene1924aab2009-11-13 21:34:57 +0000639 if (VerboseAsm)
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000640 EmitComments(*MI);
641 O << '\n';
Devang Patelaf0e2722009-10-06 02:19:11 +0000642
643 processDebugLoc(MI, false);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000644}
645
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000646void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
647 if (Subtarget->isTargetDarwin()) {
648 // All darwin targets use mach-o.
649 TargetLoweringObjectFileMachO &TLOFMacho =
650 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
651
652 MachineModuleInfoMachO &MMIMacho =
653 MMI->getObjFileInfo<MachineModuleInfoMachO>();
654
655 // Output stubs for dynamically-linked functions.
656 MachineModuleInfoMachO::SymbolListTy Stubs;
657
658 Stubs = MMIMacho.GetFnStubList();
659 if (!Stubs.empty()) {
660 const MCSection *TheSection =
661 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
662 MCSectionMachO::S_SYMBOL_STUBS |
663 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
664 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
665 5, SectionKind::getMetadata());
666 OutStreamer.SwitchSection(TheSection);
667
668 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000669 O << *Stubs[i].first << ":\n";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000670 // Get the MCSymbol without the $stub suffix.
Chris Lattner10b318b2010-01-17 21:43:43 +0000671 O << "\t.indirect_symbol " << *Stubs[i].second;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000672 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
673 }
674 O << '\n';
675
676 Stubs.clear();
677 }
678
679 // Output stubs for external and common global variables.
680 Stubs = MMIMacho.GetGVStubList();
681 if (!Stubs.empty()) {
682 const MCSection *TheSection =
683 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
684 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
685 SectionKind::getMetadata());
686 OutStreamer.SwitchSection(TheSection);
687
688 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000689 O << *Stubs[i].first << ":\n\t.indirect_symbol " << *Stubs[i].second;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000690 O << "\n\t.long\t0\n";
691 }
692 Stubs.clear();
693 }
694
695 Stubs = MMIMacho.GetHiddenGVStubList();
696 if (!Stubs.empty()) {
697 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
698 EmitAlignment(2);
699
700 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000701 O << *Stubs[i].first << ":\n" << MAI->getData32bitsDirective();
702 O << *Stubs[i].second << '\n';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000703 }
704 Stubs.clear();
705 }
706
707 // Funny Darwin hack: This flag tells the linker that no global symbols
708 // contain code that falls through to other global symbols (e.g. the obvious
709 // implementation of multiple entry points). If this doesn't occur, the
710 // linker can safely perform dead code stripping. Since LLVM never
711 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000712 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000713 }
714
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000715 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000716 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000717 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000718
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000719 // Emit type information for external functions
720 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000721 E = COFFMMI.stub_end(); I != E; ++I) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000722 O << "\t.def\t " << I->getKeyData()
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000723 << ";\t.scl\t" << COFF::C_EXT
724 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
725 << ";\t.endef\n";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000726 }
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000727
728 if (Subtarget->isTargetCygMing()) {
729 // Necessary for dllexport support
Chris Lattner4fb69f42010-01-16 00:51:39 +0000730 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000731
732 TargetLoweringObjectFileCOFF &TLOFCOFF =
733 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
734
735 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
736 if (I->hasDLLExportLinkage()) {
Chris Lattner5957c842010-01-18 00:59:24 +0000737 MCSymbol *Sym = GetGlobalValueSymbol(I);
Chris Lattner4fb69f42010-01-16 00:51:39 +0000738 COFFMMI.DecorateCygMingName(Sym, OutContext, I, *TM.getTargetData());
739 DLLExportedFns.push_back(Sym);
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000740 }
741
742 for (Module::const_global_iterator I = M.global_begin(),
743 E = M.global_end(); I != E; ++I)
Chris Lattner4fb69f42010-01-16 00:51:39 +0000744 if (I->hasDLLExportLinkage())
745 DLLExportedGlobals.push_back(GetGlobalValueSymbol(I));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000746
747 // Output linker support code for dllexported globals on windows.
748 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
749 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
750 true,
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000751 SectionKind::getMetadata()));
Chris Lattner10b318b2010-01-17 21:43:43 +0000752 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
753 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000754
Chris Lattner10b318b2010-01-17 21:43:43 +0000755 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
756 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000757 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000758 }
759 }
760}
761
762
763//===----------------------------------------------------------------------===//
764// Target Registry Stuff
765//===----------------------------------------------------------------------===//
766
767static MCInstPrinter *createX86MCInstPrinter(const Target &T,
768 unsigned SyntaxVariant,
769 const MCAsmInfo &MAI,
770 raw_ostream &O) {
771 if (SyntaxVariant == 0)
772 return new X86ATTInstPrinter(O, MAI);
773 if (SyntaxVariant == 1)
774 return new X86IntelInstPrinter(O, MAI);
775 return 0;
776}
777
778// Force static initialization.
779extern "C" void LLVMInitializeX86AsmPrinter() {
780 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
781 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
782
783 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
784 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
785}