blob: 025d843f4d9246ff1aa204b722824fc3ef3f914b [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
Chris Lattner14c38ec2010-01-28 01:02:27 +000011// of machine-dependent LLVM code to X86 machine code.
Chris Lattner72614082002-10-25 22:55:53 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner0dc32ea2009-09-20 07:41:30 +000015#include "X86AsmPrinter.h"
16#include "X86ATTInstPrinter.h"
17#include "X86IntelInstPrinter.h"
18#include "X86MCInstLower.h"
19#include "X86.h"
20#include "X86COFF.h"
21#include "X86COFFMachineModuleInfo.h"
22#include "X86MachineFunctionInfo.h"
23#include "X86TargetMachine.h"
24#include "llvm/CallingConv.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Type.h"
28#include "llvm/Assembly/Writer.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000029#include "llvm/MC/MCAsmInfo.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000030#include "llvm/MC/MCContext.h"
Chris Lattnerbeb42692010-02-03 06:42:38 +000031#include "llvm/MC/MCExpr.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000032#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"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000037#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000040#include "llvm/Target/Mangler.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000041#include "llvm/Target/TargetOptions.h"
42#include "llvm/Target/TargetRegistry.h"
43#include "llvm/ADT/SmallString.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000044using namespace llvm;
45
Chris Lattner0dc32ea2009-09-20 07:41:30 +000046//===----------------------------------------------------------------------===//
47// Primitive Helper Functions.
48//===----------------------------------------------------------------------===//
49
Chris Lattner0dc32ea2009-09-20 07:41:30 +000050void X86AsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner589c6f62010-01-26 06:28:43 +000051 const TargetLowering *TLI = TM.getTargetLowering();
52 O << *static_cast<const X86TargetLowering*>(TLI)->getPICBaseSymbol(MF,
53 OutContext);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000054}
55
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000056MCSymbol *X86AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
Chris Lattner2341f6c2010-03-12 19:04:14 +000057 MCSymbol *Symb = Mang->getSymbol(GV);
58
59 if (!Subtarget->isTargetCygMing() || !isa<Function>(GV))
60 return Symb;
61
62 X86COFFMachineModuleInfo &COFFMMI =
63 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
64 COFFMMI.DecorateCygMingName(Symb, OutContext, GV, *TM.getTargetData());
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000065
Chris Lattner2341f6c2010-03-12 19:04:14 +000066 // Save function name for later type emission.
67 const Function *F = cast<Function>(GV);
68 if (F->isDeclaration())
69 COFFMMI.addExternalFunction(Symb->getName());
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000070
71 return Symb;
72}
73
Chris Lattner14c38ec2010-01-28 01:02:27 +000074/// runOnMachineFunction - Emit the function body.
Chris Lattner0dc32ea2009-09-20 07:41:30 +000075///
76bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +000077 SetupMachineFunction(MF);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000078
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000079 if (Subtarget->isTargetCOFF()) {
Chris Lattnerb11caed2010-01-26 23:18:44 +000080 const Function *F = MF.getFunction();
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000081 O << "\t.def\t " << *CurrentFnSym << ";\t.scl\t" <<
Chris Lattnerb11caed2010-01-26 23:18:44 +000082 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
83 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
84 << ";\t.endef\n";
85 }
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000086
Chris Lattnerb11caed2010-01-26 23:18:44 +000087 // Have common code print out the function header with linkage info etc.
88 EmitFunctionHeader();
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000089
Chris Lattner14c38ec2010-01-28 01:02:27 +000090 // Emit the rest of the function body.
91 EmitFunctionBody();
Chris Lattner0dc32ea2009-09-20 07:41:30 +000092
93 // We didn't modify anything.
94 return false;
95}
96
97/// printSymbolOperand - Print a raw symbol reference operand. This handles
98/// jump tables, constant pools, global address and external symbols, all of
99/// which print to a label with various suffixes for relocation types etc.
100void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) {
101 switch (MO.getType()) {
102 default: llvm_unreachable("unknown symbol type!");
103 case MachineOperand::MO_JumpTableIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +0000104 O << *GetJTISymbol(MO.getIndex());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000105 break;
106 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +0000107 O << *GetCPISymbol(MO.getIndex());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000108 printOffset(MO.getOffset());
109 break;
110 case MachineOperand::MO_GlobalAddress: {
111 const GlobalValue *GV = MO.getGlobal();
112
Chris Lattner5957c842010-01-18 00:59:24 +0000113 MCSymbol *GVSym;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000114 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattner7a2ba942010-01-16 18:37:32 +0000115 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000116 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
117 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
118 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattner7a2ba942010-01-16 18:37:32 +0000119 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner4fb69f42010-01-16 00:51:39 +0000120 else
121 GVSym = GetGlobalValueSymbol(GV);
122
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000123 // Handle dllimport linkage.
124 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner4fb69f42010-01-16 00:51:39 +0000125 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000126
127 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
128 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000129 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000130 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000131 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000132 if (StubSym.getPointer() == 0)
133 StubSym = MachineModuleInfoImpl::
134 StubValueTy(GetGlobalValueSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000135 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattner7a2ba942010-01-16 18:37:32 +0000136 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000137 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000138 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000139 if (StubSym.getPointer() == 0)
140 StubSym = MachineModuleInfoImpl::
141 StubValueTy(GetGlobalValueSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000142 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000143 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingcebae362010-03-10 22:34:10 +0000144 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000145 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000146 if (StubSym.getPointer() == 0)
147 StubSym = MachineModuleInfoImpl::
148 StubValueTy(GetGlobalValueSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000149 }
150
151 // If the name begins with a dollar-sign, enclose it in parens. We do this
152 // to avoid having it look like an integer immediate to the assembler.
Chris Lattner4fb69f42010-01-16 00:51:39 +0000153 if (GVSym->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000154 O << *GVSym;
155 else
156 O << '(' << *GVSym << ')';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000157 printOffset(MO.getOffset());
158 break;
159 }
160 case MachineOperand::MO_ExternalSymbol: {
Chris Lattneree9250b2010-01-13 07:56:59 +0000161 const MCSymbol *SymToPrint;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000162 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner8570f592010-01-16 01:00:27 +0000163 SmallString<128> TempNameStr;
164 TempNameStr += StringRef(MO.getSymbolName());
165 TempNameStr += StringRef("$stub");
166
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000167 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingcebae362010-03-10 22:34:10 +0000168 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000169 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000170 if (StubSym.getPointer() == 0) {
Chris Lattner48130352010-01-13 06:38:18 +0000171 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
Bill Wendlingcebae362010-03-10 22:34:10 +0000172 StubSym = MachineModuleInfoImpl::
173 StubValueTy(OutContext.GetOrCreateSymbol(TempNameStr.str()),
174 true);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000175 }
Bill Wendlingcebae362010-03-10 22:34:10 +0000176 SymToPrint = StubSym.getPointer();
Chris Lattneree9250b2010-01-13 07:56:59 +0000177 } else {
Chris Lattner8570f592010-01-16 01:00:27 +0000178 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000179 }
180
181 // If the name begins with a dollar-sign, enclose it in parens. We do this
182 // to avoid having it look like an integer immediate to the assembler.
Chris Lattneree9250b2010-01-13 07:56:59 +0000183 if (SymToPrint->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000184 O << *SymToPrint;
185 else
186 O << '(' << *SymToPrint << '(';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000187 break;
188 }
189 }
190
191 switch (MO.getTargetFlags()) {
192 default:
193 llvm_unreachable("Unknown target flag on GV operand");
194 case X86II::MO_NO_FLAG: // No flag.
195 break;
196 case X86II::MO_DARWIN_NONLAZY:
197 case X86II::MO_DLLIMPORT:
198 case X86II::MO_DARWIN_STUB:
199 // These affect the name of the symbol, not any suffix.
200 break;
201 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
202 O << " + [.-";
203 PrintPICBaseSymbol();
204 O << ']';
205 break;
206 case X86II::MO_PIC_BASE_OFFSET:
207 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
208 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
209 O << '-';
210 PrintPICBaseSymbol();
211 break;
212 case X86II::MO_TLSGD: O << "@TLSGD"; break;
213 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
214 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
215 case X86II::MO_TPOFF: O << "@TPOFF"; break;
216 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
217 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
218 case X86II::MO_GOT: O << "@GOT"; break;
219 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
220 case X86II::MO_PLT: O << "@PLT"; break;
221 }
222}
223
224/// print_pcrel_imm - This is used to print an immediate value that ends up
225/// being encoded as a pc-relative value. These print slightly differently, for
226/// example, a $ is not emitted.
227void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
228 const MachineOperand &MO = MI->getOperand(OpNo);
229 switch (MO.getType()) {
230 default: llvm_unreachable("Unknown pcrel immediate operand");
231 case MachineOperand::MO_Immediate:
232 O << MO.getImm();
233 return;
234 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000235 O << *MO.getMBB()->getSymbol(OutContext);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000236 return;
237 case MachineOperand::MO_GlobalAddress:
238 case MachineOperand::MO_ExternalSymbol:
239 printSymbolOperand(MO);
240 return;
241 }
242}
243
244
245void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner5957c842010-01-18 00:59:24 +0000246 const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000247 const MachineOperand &MO = MI->getOperand(OpNo);
248 switch (MO.getType()) {
249 default: llvm_unreachable("unknown operand type!");
250 case MachineOperand::MO_Register: {
251 O << '%';
252 unsigned Reg = MO.getReg();
253 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
254 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
255 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
256 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
257 Reg = getX86SubSuperRegister(Reg, VT);
258 }
259 O << X86ATTInstPrinter::getRegisterName(Reg);
260 return;
261 }
262
263 case MachineOperand::MO_Immediate:
264 O << '$' << MO.getImm();
265 return;
266
267 case MachineOperand::MO_JumpTableIndex:
268 case MachineOperand::MO_ConstantPoolIndex:
269 case MachineOperand::MO_GlobalAddress:
270 case MachineOperand::MO_ExternalSymbol: {
271 O << '$';
272 printSymbolOperand(MO);
273 break;
274 }
275 }
276}
277
278void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
279 unsigned char value = MI->getOperand(Op).getImm();
280 assert(value <= 7 && "Invalid ssecc argument!");
281 switch (value) {
282 case 0: O << "eq"; break;
283 case 1: O << "lt"; break;
284 case 2: O << "le"; break;
285 case 3: O << "unord"; break;
286 case 4: O << "neq"; break;
287 case 5: O << "nlt"; break;
288 case 6: O << "nle"; break;
289 case 7: O << "ord"; break;
290 }
291}
292
293void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
294 const char *Modifier) {
295 const MachineOperand &BaseReg = MI->getOperand(Op);
296 const MachineOperand &IndexReg = MI->getOperand(Op+2);
297 const MachineOperand &DispSpec = MI->getOperand(Op+3);
298
299 // If we really don't want to print out (rip), don't.
300 bool HasBaseReg = BaseReg.getReg() != 0;
301 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
302 BaseReg.getReg() == X86::RIP)
303 HasBaseReg = false;
304
305 // HasParenPart - True if we will print out the () part of the mem ref.
306 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
307
308 if (DispSpec.isImm()) {
309 int DispVal = DispSpec.getImm();
310 if (DispVal || !HasParenPart)
311 O << DispVal;
312 } else {
313 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
314 DispSpec.isJTI() || DispSpec.isSymbol());
315 printSymbolOperand(MI->getOperand(Op+3));
316 }
317
318 if (HasParenPart) {
319 assert(IndexReg.getReg() != X86::ESP &&
320 "X86 doesn't allow scaling by ESP");
321
322 O << '(';
323 if (HasBaseReg)
324 printOperand(MI, Op, Modifier);
325
326 if (IndexReg.getReg()) {
327 O << ',';
328 printOperand(MI, Op+2, Modifier);
329 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
330 if (ScaleVal != 1)
331 O << ',' << ScaleVal;
332 }
333 O << ')';
334 }
335}
336
337void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
338 const char *Modifier) {
339 assert(isMem(MI, Op) && "Invalid memory reference!");
340 const MachineOperand &Segment = MI->getOperand(Op+4);
341 if (Segment.getReg()) {
342 printOperand(MI, Op+4, Modifier);
343 O << ':';
344 }
345 printLeaMemReference(MI, Op, Modifier);
346}
347
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000348void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
349 PrintPICBaseSymbol();
350 O << '\n';
351 PrintPICBaseSymbol();
352 O << ':';
353}
354
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000355bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
356 unsigned Reg = MO.getReg();
357 switch (Mode) {
358 default: return true; // Unknown mode.
359 case 'b': // Print QImode register
360 Reg = getX86SubSuperRegister(Reg, MVT::i8);
361 break;
362 case 'h': // Print QImode high register
363 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
364 break;
365 case 'w': // Print HImode register
366 Reg = getX86SubSuperRegister(Reg, MVT::i16);
367 break;
368 case 'k': // Print SImode register
369 Reg = getX86SubSuperRegister(Reg, MVT::i32);
370 break;
371 case 'q': // Print DImode register
372 Reg = getX86SubSuperRegister(Reg, MVT::i64);
373 break;
374 }
375
376 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
377 return false;
378}
379
380/// PrintAsmOperand - Print out an operand for an inline asm expression.
381///
382bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
383 unsigned AsmVariant,
384 const char *ExtraCode) {
385 // Does this asm operand have a single letter operand modifier?
386 if (ExtraCode && ExtraCode[0]) {
387 if (ExtraCode[1] != 0) return true; // Unknown modifier.
388
389 const MachineOperand &MO = MI->getOperand(OpNo);
390
391 switch (ExtraCode[0]) {
392 default: return true; // Unknown modifier.
393 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
394 if (MO.isImm()) {
395 O << MO.getImm();
396 return false;
397 }
398 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
399 printSymbolOperand(MO);
400 return false;
401 }
402 if (MO.isReg()) {
403 O << '(';
404 printOperand(MI, OpNo);
405 O << ')';
406 return false;
407 }
408 return true;
409
410 case 'c': // Don't print "$" before a global var name or constant.
411 if (MO.isImm())
412 O << MO.getImm();
413 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
414 printSymbolOperand(MO);
415 else
416 printOperand(MI, OpNo);
417 return false;
418
419 case 'A': // Print '*' before a register (it must be a register)
420 if (MO.isReg()) {
421 O << '*';
422 printOperand(MI, OpNo);
423 return false;
424 }
425 return true;
426
427 case 'b': // Print QImode register
428 case 'h': // Print QImode high register
429 case 'w': // Print HImode register
430 case 'k': // Print SImode register
431 case 'q': // Print DImode register
432 if (MO.isReg())
433 return printAsmMRegister(MO, ExtraCode[0]);
434 printOperand(MI, OpNo);
435 return false;
436
437 case 'P': // This is the operand of a call, treat specially.
438 print_pcrel_imm(MI, OpNo);
439 return false;
440
441 case 'n': // Negate the immediate or print a '-' before the operand.
442 // Note: this is a temporary solution. It should be handled target
443 // independently as part of the 'MC' work.
444 if (MO.isImm()) {
445 O << -MO.getImm();
446 return false;
447 }
448 O << '-';
449 }
450 }
451
452 printOperand(MI, OpNo);
453 return false;
454}
455
456bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
457 unsigned OpNo, unsigned AsmVariant,
458 const char *ExtraCode) {
459 if (ExtraCode && ExtraCode[0]) {
460 if (ExtraCode[1] != 0) return true; // Unknown modifier.
461
462 switch (ExtraCode[0]) {
463 default: return true; // Unknown modifier.
464 case 'b': // Print QImode register
465 case 'h': // Print QImode high register
466 case 'w': // Print HImode register
467 case 'k': // Print SImode register
468 case 'q': // Print SImode register
469 // These only apply to registers, ignore on mem.
470 break;
471 case 'P': // Don't print @PLT, but do print as memory.
472 printMemReference(MI, OpNo, "no-rip");
473 return false;
474 }
475 }
476 printMemReference(MI, OpNo);
477 return false;
478}
479
480
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000481void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
482 if (Subtarget->isTargetDarwin()) {
483 // All darwin targets use mach-o.
484 TargetLoweringObjectFileMachO &TLOFMacho =
485 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
486
487 MachineModuleInfoMachO &MMIMacho =
488 MMI->getObjFileInfo<MachineModuleInfoMachO>();
489
490 // Output stubs for dynamically-linked functions.
491 MachineModuleInfoMachO::SymbolListTy Stubs;
492
493 Stubs = MMIMacho.GetFnStubList();
494 if (!Stubs.empty()) {
495 const MCSection *TheSection =
496 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
497 MCSectionMachO::S_SYMBOL_STUBS |
498 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
499 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
500 5, SectionKind::getMetadata());
501 OutStreamer.SwitchSection(TheSection);
502
503 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000504 // L_foo$stub:
505 OutStreamer.EmitLabel(Stubs[i].first);
506 // .indirect_symbol _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000507 OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
508 MCSA_IndirectSymbol);
Chris Lattnerbeb42692010-02-03 06:42:38 +0000509 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4 = -12.
510 const char HltInsts[] = { -12, -12, -12, -12, -12 };
511 OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000512 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000513
514 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000515 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000516 }
517
518 // Output stubs for external and common global variables.
519 Stubs = MMIMacho.GetGVStubList();
520 if (!Stubs.empty()) {
521 const MCSection *TheSection =
522 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
523 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
524 SectionKind::getMetadata());
525 OutStreamer.SwitchSection(TheSection);
526
527 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000528 // L_foo$non_lazy_ptr:
529 OutStreamer.EmitLabel(Stubs[i].first);
530 // .indirect_symbol _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000531 OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
532 MCSA_IndirectSymbol);
Chris Lattnerbeb42692010-02-03 06:42:38 +0000533 // .long 0
534 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000535 }
536 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000537 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000538 }
539
540 Stubs = MMIMacho.GetHiddenGVStubList();
541 if (!Stubs.empty()) {
542 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
543 EmitAlignment(2);
544
545 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000546 // L_foo$non_lazy_ptr:
547 OutStreamer.EmitLabel(Stubs[i].first);
548 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000549 OutStreamer.EmitValue(MCSymbolRefExpr::
550 Create(Stubs[i].second.getPointer(),
551 OutContext),
Chris Lattnerbeb42692010-02-03 06:42:38 +0000552 4/*size*/, 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000553 }
554 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000555 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000556 }
557
558 // Funny Darwin hack: This flag tells the linker that no global symbols
559 // contain code that falls through to other global symbols (e.g. the obvious
560 // implementation of multiple entry points). If this doesn't occur, the
561 // linker can safely perform dead code stripping. Since LLVM never
562 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000563 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000564 }
565
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000566 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000567 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000568 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000569
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000570 // Emit type information for external functions
571 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000572 E = COFFMMI.stub_end(); I != E; ++I) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000573 O << "\t.def\t " << I->getKeyData()
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000574 << ";\t.scl\t" << COFF::C_EXT
575 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
576 << ";\t.endef\n";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000577 }
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000578
579 if (Subtarget->isTargetCygMing()) {
580 // Necessary for dllexport support
Chris Lattner4fb69f42010-01-16 00:51:39 +0000581 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000582
583 TargetLoweringObjectFileCOFF &TLOFCOFF =
584 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
585
586 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
587 if (I->hasDLLExportLinkage()) {
Chris Lattner5957c842010-01-18 00:59:24 +0000588 MCSymbol *Sym = GetGlobalValueSymbol(I);
Chris Lattner4fb69f42010-01-16 00:51:39 +0000589 DLLExportedFns.push_back(Sym);
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000590 }
591
592 for (Module::const_global_iterator I = M.global_begin(),
593 E = M.global_end(); I != E; ++I)
Chris Lattner4fb69f42010-01-16 00:51:39 +0000594 if (I->hasDLLExportLinkage())
595 DLLExportedGlobals.push_back(GetGlobalValueSymbol(I));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000596
597 // Output linker support code for dllexported globals on windows.
598 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
599 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
600 true,
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000601 SectionKind::getMetadata()));
Chris Lattner10b318b2010-01-17 21:43:43 +0000602 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
603 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000604
Chris Lattner10b318b2010-01-17 21:43:43 +0000605 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
606 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000607 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000608 }
609 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000610
611 if (Subtarget->isTargetELF()) {
612 TargetLoweringObjectFileELF &TLOFELF =
613 static_cast<TargetLoweringObjectFileELF &>(getObjFileLowering());
614
615 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
616
617 // Output stubs for external and common global variables.
618 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
619 if (!Stubs.empty()) {
620 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
621 const TargetData *TD = TM.getTargetData();
622
623 for (unsigned i = 0, e = Stubs.size(); i != e; ++i)
624 O << *Stubs[i].first << ":\n"
625 << (TD->getPointerSize() == 8 ?
626 MAI->getData64bitsDirective() : MAI->getData32bitsDirective())
Bill Wendlingcebae362010-03-10 22:34:10 +0000627 << *Stubs[i].second.getPointer() << '\n';
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000628
629 Stubs.clear();
630 }
631 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000632}
633
634
635//===----------------------------------------------------------------------===//
636// Target Registry Stuff
637//===----------------------------------------------------------------------===//
638
639static MCInstPrinter *createX86MCInstPrinter(const Target &T,
640 unsigned SyntaxVariant,
641 const MCAsmInfo &MAI,
642 raw_ostream &O) {
643 if (SyntaxVariant == 0)
644 return new X86ATTInstPrinter(O, MAI);
645 if (SyntaxVariant == 1)
646 return new X86IntelInstPrinter(O, MAI);
647 return 0;
648}
649
650// Force static initialization.
651extern "C" void LLVMInitializeX86AsmPrinter() {
652 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
653 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
654
655 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
656 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
657}