blob: b3654971a520eb17b9dbd9bdf88ff80e6b88efb9 [file] [log] [blame]
Chris Lattnerbf4b6a82009-09-20 07:41:30 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly --------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
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//
Chris Lattnerbf4b6a82009-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'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattnerbf4b6a82009-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 Lattnerbf4b6a82009-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 Lattnerce409842010-01-17 21:43:43 +000063 O << *X86MCInstLower(OutContext, 0, *AP).GetPICBaseSymbol();
Chris Lattnerbf4b6a82009-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 Lattnerc53c24c2010-01-16 00:51:39 +000073 COFFMMI.DecorateCygMingName(CurrentFnSym, OutContext, F,
74 *TM.getTargetData());
Chris Lattnerbf4b6a82009-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 Lattner2d7c8142010-01-23 06:39:22 +000087 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_Global);
Chris Lattnerbf4b6a82009-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 Lattner2d7c8142010-01-23 06:39:22 +000095 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_Global);
Chris Lattnerce409842010-01-17 21:43:43 +000096 O << MAI->getWeakDefDirective() << *CurrentFnSym << '\n';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000097 } else if (Subtarget->isTargetCygMing()) {
Chris Lattner2d7c8142010-01-23 06:39:22 +000098 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_Global);
Chris Lattner59be4ea2010-01-25 18:58:59 +000099 // FIXME: linkonce should be a section attribute, handled by COFF Section
100 // assignment.
101 // http://sourceware.org/binutils/docs-2.20/as/Linkonce.html#Linkonce
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000102 O << "\t.linkonce discard\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000103 } else {
Chris Lattnerce409842010-01-17 21:43:43 +0000104 O << "\t.weak\t" << *CurrentFnSym << '\n';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000105 }
106 break;
107 }
108
Chris Lattner86ec9712010-01-16 00:32:38 +0000109 printVisibility(CurrentFnSym, F->getVisibility());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000110
Chris Lattner59be4ea2010-01-25 18:58:59 +0000111 if (MAI->hasDotTypeDotSizeDirective()) {
Chris Lattner9f8ef902010-01-25 18:33:40 +0000112 OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
Chris Lattner86ec9712010-01-16 00:32:38 +0000113 } else if (Subtarget->isTargetCygMing()) {
Chris Lattnerce409842010-01-17 21:43:43 +0000114 O << "\t.def\t " << *CurrentFnSym;
Chris Lattner86ec9712010-01-16 00:32:38 +0000115 O << ";\t.scl\t" <<
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000116 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
117 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
118 << ";\t.endef\n";
119 }
120
Chris Lattnerce409842010-01-17 21:43:43 +0000121 O << *CurrentFnSym << ':';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000122 if (VerboseAsm) {
123 O.PadToColumn(MAI->getCommentColumn());
124 O << MAI->getCommentString() << ' ';
125 WriteAsOperand(O, F, /*PrintType=*/false, F->getParent());
126 }
127 O << '\n';
128
129 // Add some workaround for linkonce linkage on Cygwin\MinGW
130 if (Subtarget->isTargetCygMing() &&
Chris Lattnerce409842010-01-17 21:43:43 +0000131 (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
132 O << "Lllvm$workaround$fake$stub$" << *CurrentFnSym << ":\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000133}
134
135/// runOnMachineFunction - This uses the printMachineInstruction()
136/// method to print assembly for each instruction.
137///
138bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
139 const Function *F = MF.getFunction();
140 this->MF = &MF;
141 CallingConv::ID CC = F->getCallingConv();
142
143 SetupMachineFunction(MF);
144 O << "\n\n";
145
146 if (Subtarget->isTargetCOFF()) {
147 X86COFFMachineModuleInfo &COFFMMI =
148 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
149
150 // Populate function information map. Don't want to populate
151 // non-stdcall or non-fastcall functions' information right now.
152 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
153 COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>());
154 }
155
156 // Print out constants referenced by the function
157 EmitConstantPool(MF.getConstantPool());
158
159 // Print the 'header' of function
160 emitFunctionHeader(MF);
161
162 // Emit pre-function debug and/or EH information.
163 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
164 DW->BeginFunction(&MF);
165
166 // Print out code for the function.
167 bool hasAnyRealCode = false;
168 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
169 I != E; ++I) {
170 // Print a label for the basic block.
Dan Gohman5fda4342009-10-06 17:38:38 +0000171 EmitBasicBlockStart(I);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000172 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
173 II != IE; ++II) {
174 // Print the assembly for the instruction.
175 if (!II->isLabel())
176 hasAnyRealCode = true;
177 printMachineInstruction(II);
178 }
179 }
180
181 if (Subtarget->isTargetDarwin() && !hasAnyRealCode) {
182 // If the function is empty, then we need to emit *something*. Otherwise,
183 // the function's label might be associated with something that it wasn't
184 // meant to be associated with. We emit a noop in this situation.
185 // We are assuming inline asms are code.
186 O << "\tnop\n";
187 }
188
Chris Lattnerce409842010-01-17 21:43:43 +0000189 if (MAI->hasDotTypeDotSizeDirective())
190 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000191
192 // Emit post-function debug information.
193 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
194 DW->EndFunction(&MF);
195
196 // Print out jump tables referenced by the function.
197 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
198
199 // We didn't modify anything.
200 return false;
201}
202
203/// printSymbolOperand - Print a raw symbol reference operand. This handles
204/// jump tables, constant pools, global address and external symbols, all of
205/// which print to a label with various suffixes for relocation types etc.
206void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) {
207 switch (MO.getType()) {
208 default: llvm_unreachable("unknown symbol type!");
209 case MachineOperand::MO_JumpTableIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000210 O << *GetJTISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000211 break;
212 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000213 O << *GetCPISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000214 printOffset(MO.getOffset());
215 break;
216 case MachineOperand::MO_GlobalAddress: {
217 const GlobalValue *GV = MO.getGlobal();
218
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000219 MCSymbol *GVSym;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000220 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000221 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000222 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
223 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
224 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000225 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000226 else
227 GVSym = GetGlobalValueSymbol(GV);
228
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000229 if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000230 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000231 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000232 COFFMMI.DecorateCygMingName(GVSym, OutContext, GV, *TM.getTargetData());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000233 }
234
235 // Handle dllimport linkage.
236 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000237 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000238
239 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
240 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000241 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000242
243 const MCSymbol *&StubSym =
244 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000245 if (StubSym == 0)
246 StubSym = GetGlobalValueSymbol(GV);
247
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000248 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattnera6f2a102010-01-16 18:37:32 +0000249 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000250 const MCSymbol *&StubSym =
251 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000252 if (StubSym == 0)
253 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000254 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000255 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000256 const MCSymbol *&StubSym =
257 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000258 if (StubSym == 0)
259 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000260 }
261
262 // If the name begins with a dollar-sign, enclose it in parens. We do this
263 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000264 if (GVSym->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000265 O << *GVSym;
266 else
267 O << '(' << *GVSym << ')';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000268 printOffset(MO.getOffset());
269 break;
270 }
271 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf80743e2010-01-13 07:56:59 +0000272 const MCSymbol *SymToPrint;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000273 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner178a9322010-01-16 01:00:27 +0000274 SmallString<128> TempNameStr;
275 TempNameStr += StringRef(MO.getSymbolName());
276 TempNameStr += StringRef("$stub");
277
278 const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000279 const MCSymbol *&StubSym =
280 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
281 if (StubSym == 0) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000282 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
283 StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000284 }
Chris Lattnerf80743e2010-01-13 07:56:59 +0000285 SymToPrint = StubSym;
286 } else {
Chris Lattner178a9322010-01-16 01:00:27 +0000287 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000288 }
289
290 // If the name begins with a dollar-sign, enclose it in parens. We do this
291 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerf80743e2010-01-13 07:56:59 +0000292 if (SymToPrint->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000293 O << *SymToPrint;
294 else
295 O << '(' << *SymToPrint << '(';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000296 break;
297 }
298 }
299
300 switch (MO.getTargetFlags()) {
301 default:
302 llvm_unreachable("Unknown target flag on GV operand");
303 case X86II::MO_NO_FLAG: // No flag.
304 break;
305 case X86II::MO_DARWIN_NONLAZY:
306 case X86II::MO_DLLIMPORT:
307 case X86II::MO_DARWIN_STUB:
308 // These affect the name of the symbol, not any suffix.
309 break;
310 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
311 O << " + [.-";
312 PrintPICBaseSymbol();
313 O << ']';
314 break;
315 case X86II::MO_PIC_BASE_OFFSET:
316 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
317 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
318 O << '-';
319 PrintPICBaseSymbol();
320 break;
321 case X86II::MO_TLSGD: O << "@TLSGD"; break;
322 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
323 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
324 case X86II::MO_TPOFF: O << "@TPOFF"; break;
325 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
326 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
327 case X86II::MO_GOT: O << "@GOT"; break;
328 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
329 case X86II::MO_PLT: O << "@PLT"; break;
330 }
331}
332
333/// print_pcrel_imm - This is used to print an immediate value that ends up
334/// being encoded as a pc-relative value. These print slightly differently, for
335/// example, a $ is not emitted.
336void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
337 const MachineOperand &MO = MI->getOperand(OpNo);
338 switch (MO.getType()) {
339 default: llvm_unreachable("Unknown pcrel immediate operand");
340 case MachineOperand::MO_Immediate:
341 O << MO.getImm();
342 return;
343 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerce409842010-01-17 21:43:43 +0000344 O << *GetMBBSymbol(MO.getMBB()->getNumber());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000345 return;
346 case MachineOperand::MO_GlobalAddress:
347 case MachineOperand::MO_ExternalSymbol:
348 printSymbolOperand(MO);
349 return;
350 }
351}
352
353
354void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000355 const char *Modifier) {
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000356 const MachineOperand &MO = MI->getOperand(OpNo);
357 switch (MO.getType()) {
358 default: llvm_unreachable("unknown operand type!");
359 case MachineOperand::MO_Register: {
360 O << '%';
361 unsigned Reg = MO.getReg();
362 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
363 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
364 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
365 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
366 Reg = getX86SubSuperRegister(Reg, VT);
367 }
368 O << X86ATTInstPrinter::getRegisterName(Reg);
369 return;
370 }
371
372 case MachineOperand::MO_Immediate:
373 O << '$' << MO.getImm();
374 return;
375
376 case MachineOperand::MO_JumpTableIndex:
377 case MachineOperand::MO_ConstantPoolIndex:
378 case MachineOperand::MO_GlobalAddress:
379 case MachineOperand::MO_ExternalSymbol: {
380 O << '$';
381 printSymbolOperand(MO);
382 break;
383 }
384 }
385}
386
387void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
388 unsigned char value = MI->getOperand(Op).getImm();
389 assert(value <= 7 && "Invalid ssecc argument!");
390 switch (value) {
391 case 0: O << "eq"; break;
392 case 1: O << "lt"; break;
393 case 2: O << "le"; break;
394 case 3: O << "unord"; break;
395 case 4: O << "neq"; break;
396 case 5: O << "nlt"; break;
397 case 6: O << "nle"; break;
398 case 7: O << "ord"; break;
399 }
400}
401
402void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
403 const char *Modifier) {
404 const MachineOperand &BaseReg = MI->getOperand(Op);
405 const MachineOperand &IndexReg = MI->getOperand(Op+2);
406 const MachineOperand &DispSpec = MI->getOperand(Op+3);
407
408 // If we really don't want to print out (rip), don't.
409 bool HasBaseReg = BaseReg.getReg() != 0;
410 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
411 BaseReg.getReg() == X86::RIP)
412 HasBaseReg = false;
413
414 // HasParenPart - True if we will print out the () part of the mem ref.
415 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
416
417 if (DispSpec.isImm()) {
418 int DispVal = DispSpec.getImm();
419 if (DispVal || !HasParenPart)
420 O << DispVal;
421 } else {
422 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
423 DispSpec.isJTI() || DispSpec.isSymbol());
424 printSymbolOperand(MI->getOperand(Op+3));
425 }
426
427 if (HasParenPart) {
428 assert(IndexReg.getReg() != X86::ESP &&
429 "X86 doesn't allow scaling by ESP");
430
431 O << '(';
432 if (HasBaseReg)
433 printOperand(MI, Op, Modifier);
434
435 if (IndexReg.getReg()) {
436 O << ',';
437 printOperand(MI, Op+2, Modifier);
438 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
439 if (ScaleVal != 1)
440 O << ',' << ScaleVal;
441 }
442 O << ')';
443 }
444}
445
446void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
447 const char *Modifier) {
448 assert(isMem(MI, Op) && "Invalid memory reference!");
449 const MachineOperand &Segment = MI->getOperand(Op+4);
450 if (Segment.getReg()) {
451 printOperand(MI, Op+4, Modifier);
452 O << ':';
453 }
454 printLeaMemReference(MI, Op, Modifier);
455}
456
457void X86AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
458 const MachineBasicBlock *MBB) const {
459 if (!MAI->getSetDirective())
460 return;
461
462 // We don't need .set machinery if we have GOT-style relocations
463 if (Subtarget->isPICStyleGOT())
464 return;
465
466 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
467 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
468
Chris Lattnerf11b3ed2010-01-18 00:21:06 +0000469 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000470
471 if (Subtarget->isPICStyleRIPRel())
Chris Lattner242edfc2010-01-23 05:26:25 +0000472 O << '-' << *GetJTISymbol(uid) << '\n';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000473 else {
474 O << '-';
475 PrintPICBaseSymbol();
476 O << '\n';
477 }
478}
479
480
481void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
482 PrintPICBaseSymbol();
483 O << '\n';
484 PrintPICBaseSymbol();
485 O << ':';
486}
487
488void X86AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
489 const MachineBasicBlock *MBB,
490 unsigned uid) const {
491 const char *JTEntryDirective = MJTI->getEntrySize() == 4 ?
492 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
493
494 O << JTEntryDirective << ' ';
495
496 if (Subtarget->isPICStyleRIPRel() || Subtarget->isPICStyleStubPIC()) {
497 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
498 << '_' << uid << "_set_" << MBB->getNumber();
Chris Lattnerce409842010-01-17 21:43:43 +0000499 } else if (Subtarget->isPICStyleGOT())
500 O << *GetMBBSymbol(MBB->getNumber()) << "@GOTOFF";
501 else
502 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000503}
504
505bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
506 unsigned Reg = MO.getReg();
507 switch (Mode) {
508 default: return true; // Unknown mode.
509 case 'b': // Print QImode register
510 Reg = getX86SubSuperRegister(Reg, MVT::i8);
511 break;
512 case 'h': // Print QImode high register
513 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
514 break;
515 case 'w': // Print HImode register
516 Reg = getX86SubSuperRegister(Reg, MVT::i16);
517 break;
518 case 'k': // Print SImode register
519 Reg = getX86SubSuperRegister(Reg, MVT::i32);
520 break;
521 case 'q': // Print DImode register
522 Reg = getX86SubSuperRegister(Reg, MVT::i64);
523 break;
524 }
525
526 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
527 return false;
528}
529
530/// PrintAsmOperand - Print out an operand for an inline asm expression.
531///
532bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
533 unsigned AsmVariant,
534 const char *ExtraCode) {
535 // Does this asm operand have a single letter operand modifier?
536 if (ExtraCode && ExtraCode[0]) {
537 if (ExtraCode[1] != 0) return true; // Unknown modifier.
538
539 const MachineOperand &MO = MI->getOperand(OpNo);
540
541 switch (ExtraCode[0]) {
542 default: return true; // Unknown modifier.
543 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
544 if (MO.isImm()) {
545 O << MO.getImm();
546 return false;
547 }
548 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
549 printSymbolOperand(MO);
550 return false;
551 }
552 if (MO.isReg()) {
553 O << '(';
554 printOperand(MI, OpNo);
555 O << ')';
556 return false;
557 }
558 return true;
559
560 case 'c': // Don't print "$" before a global var name or constant.
561 if (MO.isImm())
562 O << MO.getImm();
563 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
564 printSymbolOperand(MO);
565 else
566 printOperand(MI, OpNo);
567 return false;
568
569 case 'A': // Print '*' before a register (it must be a register)
570 if (MO.isReg()) {
571 O << '*';
572 printOperand(MI, OpNo);
573 return false;
574 }
575 return true;
576
577 case 'b': // Print QImode register
578 case 'h': // Print QImode high register
579 case 'w': // Print HImode register
580 case 'k': // Print SImode register
581 case 'q': // Print DImode register
582 if (MO.isReg())
583 return printAsmMRegister(MO, ExtraCode[0]);
584 printOperand(MI, OpNo);
585 return false;
586
587 case 'P': // This is the operand of a call, treat specially.
588 print_pcrel_imm(MI, OpNo);
589 return false;
590
591 case 'n': // Negate the immediate or print a '-' before the operand.
592 // Note: this is a temporary solution. It should be handled target
593 // independently as part of the 'MC' work.
594 if (MO.isImm()) {
595 O << -MO.getImm();
596 return false;
597 }
598 O << '-';
599 }
600 }
601
602 printOperand(MI, OpNo);
603 return false;
604}
605
606bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
607 unsigned OpNo, unsigned AsmVariant,
608 const char *ExtraCode) {
609 if (ExtraCode && ExtraCode[0]) {
610 if (ExtraCode[1] != 0) return true; // Unknown modifier.
611
612 switch (ExtraCode[0]) {
613 default: return true; // Unknown modifier.
614 case 'b': // Print QImode register
615 case 'h': // Print QImode high register
616 case 'w': // Print HImode register
617 case 'k': // Print SImode register
618 case 'q': // Print SImode register
619 // These only apply to registers, ignore on mem.
620 break;
621 case 'P': // Don't print @PLT, but do print as memory.
622 printMemReference(MI, OpNo, "no-rip");
623 return false;
624 }
625 }
626 printMemReference(MI, OpNo);
627 return false;
628}
629
630
631
632/// printMachineInstruction -- Print out a single X86 LLVM instruction MI in
633/// AT&T syntax to the current output stream.
634///
635void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
636 ++EmittedInsts;
637
Devang Patel5450fc12009-10-06 02:19:11 +0000638 processDebugLoc(MI, true);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000639
640 printInstructionThroughMCStreamer(MI);
641
David Greeneca9b04b2009-11-13 21:34:57 +0000642 if (VerboseAsm)
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000643 EmitComments(*MI);
644 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000645
646 processDebugLoc(MI, false);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000647}
648
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000649void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
650 if (Subtarget->isTargetDarwin()) {
651 // All darwin targets use mach-o.
652 TargetLoweringObjectFileMachO &TLOFMacho =
653 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
654
655 MachineModuleInfoMachO &MMIMacho =
656 MMI->getObjFileInfo<MachineModuleInfoMachO>();
657
658 // Output stubs for dynamically-linked functions.
659 MachineModuleInfoMachO::SymbolListTy Stubs;
660
661 Stubs = MMIMacho.GetFnStubList();
662 if (!Stubs.empty()) {
663 const MCSection *TheSection =
664 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
665 MCSectionMachO::S_SYMBOL_STUBS |
666 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
667 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
668 5, SectionKind::getMetadata());
669 OutStreamer.SwitchSection(TheSection);
670
671 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000672 O << *Stubs[i].first << ":\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000673 // Get the MCSymbol without the $stub suffix.
Chris Lattnerce409842010-01-17 21:43:43 +0000674 O << "\t.indirect_symbol " << *Stubs[i].second;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000675 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
676 }
677 O << '\n';
678
679 Stubs.clear();
680 }
681
682 // Output stubs for external and common global variables.
683 Stubs = MMIMacho.GetGVStubList();
684 if (!Stubs.empty()) {
685 const MCSection *TheSection =
686 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
687 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
688 SectionKind::getMetadata());
689 OutStreamer.SwitchSection(TheSection);
690
691 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000692 O << *Stubs[i].first << ":\n\t.indirect_symbol " << *Stubs[i].second;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000693 O << "\n\t.long\t0\n";
694 }
695 Stubs.clear();
696 }
697
698 Stubs = MMIMacho.GetHiddenGVStubList();
699 if (!Stubs.empty()) {
700 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
701 EmitAlignment(2);
702
703 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000704 O << *Stubs[i].first << ":\n" << MAI->getData32bitsDirective();
705 O << *Stubs[i].second << '\n';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000706 }
707 Stubs.clear();
708 }
709
710 // Funny Darwin hack: This flag tells the linker that no global symbols
711 // contain code that falls through to other global symbols (e.g. the obvious
712 // implementation of multiple entry points). If this doesn't occur, the
713 // linker can safely perform dead code stripping. Since LLVM never
714 // generates code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000715 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000716 }
717
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000718 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000719 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000720 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000721
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000722 // Emit type information for external functions
723 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000724 E = COFFMMI.stub_end(); I != E; ++I) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000725 O << "\t.def\t " << I->getKeyData()
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000726 << ";\t.scl\t" << COFF::C_EXT
727 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
728 << ";\t.endef\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000729 }
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000730
731 if (Subtarget->isTargetCygMing()) {
732 // Necessary for dllexport support
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000733 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000734
735 TargetLoweringObjectFileCOFF &TLOFCOFF =
736 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
737
738 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
739 if (I->hasDLLExportLinkage()) {
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000740 MCSymbol *Sym = GetGlobalValueSymbol(I);
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000741 COFFMMI.DecorateCygMingName(Sym, OutContext, I, *TM.getTargetData());
742 DLLExportedFns.push_back(Sym);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000743 }
744
745 for (Module::const_global_iterator I = M.global_begin(),
746 E = M.global_end(); I != E; ++I)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000747 if (I->hasDLLExportLinkage())
748 DLLExportedGlobals.push_back(GetGlobalValueSymbol(I));
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000749
750 // Output linker support code for dllexported globals on windows.
751 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
752 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
753 true,
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000754 SectionKind::getMetadata()));
Chris Lattnerce409842010-01-17 21:43:43 +0000755 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
756 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000757
Chris Lattnerce409842010-01-17 21:43:43 +0000758 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
759 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000760 }
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000761 }
762 }
763}
764
765
766//===----------------------------------------------------------------------===//
767// Target Registry Stuff
768//===----------------------------------------------------------------------===//
769
770static MCInstPrinter *createX86MCInstPrinter(const Target &T,
771 unsigned SyntaxVariant,
772 const MCAsmInfo &MAI,
773 raw_ostream &O) {
774 if (SyntaxVariant == 0)
775 return new X86ATTInstPrinter(O, MAI);
776 if (SyntaxVariant == 1)
777 return new X86IntelInstPrinter(O, MAI);
778 return 0;
779}
780
781// Force static initialization.
782extern "C" void LLVMInitializeX86AsmPrinter() {
783 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
784 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
785
786 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
787 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
788}