blob: 8037247fefa4f2b2d1cf95cdd97a43db229a647d [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
Chris Lattnerd7dede22010-01-28 01:02:27 +000011// of machine-dependent LLVM code to X86 machine code.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbf4b6a82009-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"
29#include "llvm/MC/MCContext.h"
30#include "llvm/MC/MCSectionMachO.h"
31#include "llvm/MC/MCStreamer.h"
32#include "llvm/MC/MCSymbol.h"
33#include "llvm/CodeGen/MachineJumpTableInfo.h"
34#include "llvm/CodeGen/MachineModuleInfoImpls.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000037#include "llvm/MC/MCAsmInfo.h"
38#include "llvm/Target/TargetLoweringObjectFile.h"
39#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegistry.h"
41#include "llvm/ADT/SmallString.h"
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000042using namespace llvm;
43
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000044//===----------------------------------------------------------------------===//
45// Primitive Helper Functions.
46//===----------------------------------------------------------------------===//
47
48void X86AsmPrinter::printMCInst(const MCInst *MI) {
49 if (MAI->getAssemblerDialect() == 0)
50 X86ATTInstPrinter(O, *MAI).printInstruction(MI);
51 else
52 X86IntelInstPrinter(O, *MAI).printInstruction(MI);
53}
54
55void X86AsmPrinter::PrintPICBaseSymbol() const {
Chris Lattner541d8902010-01-26 06:28:43 +000056 const TargetLowering *TLI = TM.getTargetLowering();
57 O << *static_cast<const X86TargetLowering*>(TLI)->getPICBaseSymbol(MF,
58 OutContext);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000059}
60
Chris Lattnerd7dede22010-01-28 01:02:27 +000061/// runOnMachineFunction - Emit the function body.
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000062///
63bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000064 SetupMachineFunction(MF);
65 O << "\n\n";
Chris Lattnere5980672010-01-26 23:18:44 +000066
67 // COFF and Cygwin specific mangling stuff. This should be moved out to the
68 // mangler or handled some other way?
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000069 if (Subtarget->isTargetCOFF()) {
70 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnere5980672010-01-26 23:18:44 +000071 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000072
73 // Populate function information map. Don't want to populate
74 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere5980672010-01-26 23:18:44 +000075 const Function *F = MF.getFunction();
76 CallingConv::ID CC = F->getCallingConv();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000077 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
78 COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>());
79 }
Chris Lattnere5980672010-01-26 23:18:44 +000080 if (Subtarget->isTargetCygMing()) {
81 const Function *F = MF.getFunction();
82 X86COFFMachineModuleInfo &COFFMMI =
83 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
84 COFFMMI.DecorateCygMingName(CurrentFnSym, OutContext,F,*TM.getTargetData());
85
86 O << "\t.def\t " << *CurrentFnSym;
87 O << ";\t.scl\t" <<
88 (F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT)
89 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
90 << ";\t.endef\n";
91 }
92
93 // Have common code print out the function header with linkage info etc.
94 EmitFunctionHeader();
95
Chris Lattnerd7dede22010-01-28 01:02:27 +000096 // Emit the rest of the function body.
97 EmitFunctionBody();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +000098
99 // We didn't modify anything.
100 return false;
101}
102
103/// printSymbolOperand - Print a raw symbol reference operand. This handles
104/// jump tables, constant pools, global address and external symbols, all of
105/// which print to a label with various suffixes for relocation types etc.
106void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO) {
107 switch (MO.getType()) {
108 default: llvm_unreachable("unknown symbol type!");
109 case MachineOperand::MO_JumpTableIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000110 O << *GetJTISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000111 break;
112 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner242edfc2010-01-23 05:26:25 +0000113 O << *GetCPISymbol(MO.getIndex());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000114 printOffset(MO.getOffset());
115 break;
116 case MachineOperand::MO_GlobalAddress: {
117 const GlobalValue *GV = MO.getGlobal();
118
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000119 MCSymbol *GVSym;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000120 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000121 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000122 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
123 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
124 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattnera6f2a102010-01-16 18:37:32 +0000125 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000126 else
127 GVSym = GetGlobalValueSymbol(GV);
128
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000129 if (Subtarget->isTargetCygMing()) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000130 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000131 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000132 COFFMMI.DecorateCygMingName(GVSym, OutContext, GV, *TM.getTargetData());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000133 }
134
135 // Handle dllimport linkage.
136 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000137 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000138
139 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
140 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000141 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000142
143 const MCSymbol *&StubSym =
144 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000145 if (StubSym == 0)
146 StubSym = GetGlobalValueSymbol(GV);
147
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000148 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattnera6f2a102010-01-16 18:37:32 +0000149 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000150 const MCSymbol *&StubSym =
151 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000152 if (StubSym == 0)
153 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000154 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000155 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000156 const MCSymbol *&StubSym =
157 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Chris Lattner178a9322010-01-16 01:00:27 +0000158 if (StubSym == 0)
159 StubSym = GetGlobalValueSymbol(GV);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000160 }
161
162 // If the name begins with a dollar-sign, enclose it in parens. We do this
163 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000164 if (GVSym->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000165 O << *GVSym;
166 else
167 O << '(' << *GVSym << ')';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000168 printOffset(MO.getOffset());
169 break;
170 }
171 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerf80743e2010-01-13 07:56:59 +0000172 const MCSymbol *SymToPrint;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000173 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner178a9322010-01-16 01:00:27 +0000174 SmallString<128> TempNameStr;
175 TempNameStr += StringRef(MO.getSymbolName());
176 TempNameStr += StringRef("$stub");
177
178 const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000179 const MCSymbol *&StubSym =
180 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
181 if (StubSym == 0) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000182 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
183 StubSym = OutContext.GetOrCreateSymbol(TempNameStr.str());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000184 }
Chris Lattnerf80743e2010-01-13 07:56:59 +0000185 SymToPrint = StubSym;
186 } else {
Chris Lattner178a9322010-01-16 01:00:27 +0000187 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000188 }
189
190 // If the name begins with a dollar-sign, enclose it in parens. We do this
191 // to avoid having it look like an integer immediate to the assembler.
Chris Lattnerf80743e2010-01-13 07:56:59 +0000192 if (SymToPrint->getName()[0] != '$')
Chris Lattnerce409842010-01-17 21:43:43 +0000193 O << *SymToPrint;
194 else
195 O << '(' << *SymToPrint << '(';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000196 break;
197 }
198 }
199
200 switch (MO.getTargetFlags()) {
201 default:
202 llvm_unreachable("Unknown target flag on GV operand");
203 case X86II::MO_NO_FLAG: // No flag.
204 break;
205 case X86II::MO_DARWIN_NONLAZY:
206 case X86II::MO_DLLIMPORT:
207 case X86II::MO_DARWIN_STUB:
208 // These affect the name of the symbol, not any suffix.
209 break;
210 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
211 O << " + [.-";
212 PrintPICBaseSymbol();
213 O << ']';
214 break;
215 case X86II::MO_PIC_BASE_OFFSET:
216 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
217 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
218 O << '-';
219 PrintPICBaseSymbol();
220 break;
221 case X86II::MO_TLSGD: O << "@TLSGD"; break;
222 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
223 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
224 case X86II::MO_TPOFF: O << "@TPOFF"; break;
225 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
226 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
227 case X86II::MO_GOT: O << "@GOT"; break;
228 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
229 case X86II::MO_PLT: O << "@PLT"; break;
230 }
231}
232
233/// print_pcrel_imm - This is used to print an immediate value that ends up
234/// being encoded as a pc-relative value. These print slightly differently, for
235/// example, a $ is not emitted.
236void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo) {
237 const MachineOperand &MO = MI->getOperand(OpNo);
238 switch (MO.getType()) {
239 default: llvm_unreachable("Unknown pcrel immediate operand");
240 case MachineOperand::MO_Immediate:
241 O << MO.getImm();
242 return;
243 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner84d5ca92010-01-26 04:55:51 +0000244 O << *MO.getMBB()->getSymbol(OutContext);
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000245 return;
246 case MachineOperand::MO_GlobalAddress:
247 case MachineOperand::MO_ExternalSymbol:
248 printSymbolOperand(MO);
249 return;
250 }
251}
252
253
254void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000255 const char *Modifier) {
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000256 const MachineOperand &MO = MI->getOperand(OpNo);
257 switch (MO.getType()) {
258 default: llvm_unreachable("unknown operand type!");
259 case MachineOperand::MO_Register: {
260 O << '%';
261 unsigned Reg = MO.getReg();
262 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
263 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
264 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
265 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
266 Reg = getX86SubSuperRegister(Reg, VT);
267 }
268 O << X86ATTInstPrinter::getRegisterName(Reg);
269 return;
270 }
271
272 case MachineOperand::MO_Immediate:
273 O << '$' << MO.getImm();
274 return;
275
276 case MachineOperand::MO_JumpTableIndex:
277 case MachineOperand::MO_ConstantPoolIndex:
278 case MachineOperand::MO_GlobalAddress:
279 case MachineOperand::MO_ExternalSymbol: {
280 O << '$';
281 printSymbolOperand(MO);
282 break;
283 }
284 }
285}
286
287void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
288 unsigned char value = MI->getOperand(Op).getImm();
289 assert(value <= 7 && "Invalid ssecc argument!");
290 switch (value) {
291 case 0: O << "eq"; break;
292 case 1: O << "lt"; break;
293 case 2: O << "le"; break;
294 case 3: O << "unord"; break;
295 case 4: O << "neq"; break;
296 case 5: O << "nlt"; break;
297 case 6: O << "nle"; break;
298 case 7: O << "ord"; break;
299 }
300}
301
302void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
303 const char *Modifier) {
304 const MachineOperand &BaseReg = MI->getOperand(Op);
305 const MachineOperand &IndexReg = MI->getOperand(Op+2);
306 const MachineOperand &DispSpec = MI->getOperand(Op+3);
307
308 // If we really don't want to print out (rip), don't.
309 bool HasBaseReg = BaseReg.getReg() != 0;
310 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
311 BaseReg.getReg() == X86::RIP)
312 HasBaseReg = false;
313
314 // HasParenPart - True if we will print out the () part of the mem ref.
315 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
316
317 if (DispSpec.isImm()) {
318 int DispVal = DispSpec.getImm();
319 if (DispVal || !HasParenPart)
320 O << DispVal;
321 } else {
322 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
323 DispSpec.isJTI() || DispSpec.isSymbol());
324 printSymbolOperand(MI->getOperand(Op+3));
325 }
326
327 if (HasParenPart) {
328 assert(IndexReg.getReg() != X86::ESP &&
329 "X86 doesn't allow scaling by ESP");
330
331 O << '(';
332 if (HasBaseReg)
333 printOperand(MI, Op, Modifier);
334
335 if (IndexReg.getReg()) {
336 O << ',';
337 printOperand(MI, Op+2, Modifier);
338 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
339 if (ScaleVal != 1)
340 O << ',' << ScaleVal;
341 }
342 O << ')';
343 }
344}
345
346void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
347 const char *Modifier) {
348 assert(isMem(MI, Op) && "Invalid memory reference!");
349 const MachineOperand &Segment = MI->getOperand(Op+4);
350 if (Segment.getReg()) {
351 printOperand(MI, Op+4, Modifier);
352 O << ':';
353 }
354 printLeaMemReference(MI, Op, Modifier);
355}
356
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000357void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
358 PrintPICBaseSymbol();
359 O << '\n';
360 PrintPICBaseSymbol();
361 O << ':';
362}
363
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000364bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode) {
365 unsigned Reg = MO.getReg();
366 switch (Mode) {
367 default: return true; // Unknown mode.
368 case 'b': // Print QImode register
369 Reg = getX86SubSuperRegister(Reg, MVT::i8);
370 break;
371 case 'h': // Print QImode high register
372 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
373 break;
374 case 'w': // Print HImode register
375 Reg = getX86SubSuperRegister(Reg, MVT::i16);
376 break;
377 case 'k': // Print SImode register
378 Reg = getX86SubSuperRegister(Reg, MVT::i32);
379 break;
380 case 'q': // Print DImode register
381 Reg = getX86SubSuperRegister(Reg, MVT::i64);
382 break;
383 }
384
385 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
386 return false;
387}
388
389/// PrintAsmOperand - Print out an operand for an inline asm expression.
390///
391bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
392 unsigned AsmVariant,
393 const char *ExtraCode) {
394 // Does this asm operand have a single letter operand modifier?
395 if (ExtraCode && ExtraCode[0]) {
396 if (ExtraCode[1] != 0) return true; // Unknown modifier.
397
398 const MachineOperand &MO = MI->getOperand(OpNo);
399
400 switch (ExtraCode[0]) {
401 default: return true; // Unknown modifier.
402 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
403 if (MO.isImm()) {
404 O << MO.getImm();
405 return false;
406 }
407 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
408 printSymbolOperand(MO);
409 return false;
410 }
411 if (MO.isReg()) {
412 O << '(';
413 printOperand(MI, OpNo);
414 O << ')';
415 return false;
416 }
417 return true;
418
419 case 'c': // Don't print "$" before a global var name or constant.
420 if (MO.isImm())
421 O << MO.getImm();
422 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
423 printSymbolOperand(MO);
424 else
425 printOperand(MI, OpNo);
426 return false;
427
428 case 'A': // Print '*' before a register (it must be a register)
429 if (MO.isReg()) {
430 O << '*';
431 printOperand(MI, OpNo);
432 return false;
433 }
434 return true;
435
436 case 'b': // Print QImode register
437 case 'h': // Print QImode high register
438 case 'w': // Print HImode register
439 case 'k': // Print SImode register
440 case 'q': // Print DImode register
441 if (MO.isReg())
442 return printAsmMRegister(MO, ExtraCode[0]);
443 printOperand(MI, OpNo);
444 return false;
445
446 case 'P': // This is the operand of a call, treat specially.
447 print_pcrel_imm(MI, OpNo);
448 return false;
449
450 case 'n': // Negate the immediate or print a '-' before the operand.
451 // Note: this is a temporary solution. It should be handled target
452 // independently as part of the 'MC' work.
453 if (MO.isImm()) {
454 O << -MO.getImm();
455 return false;
456 }
457 O << '-';
458 }
459 }
460
461 printOperand(MI, OpNo);
462 return false;
463}
464
465bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
466 unsigned OpNo, unsigned AsmVariant,
467 const char *ExtraCode) {
468 if (ExtraCode && ExtraCode[0]) {
469 if (ExtraCode[1] != 0) return true; // Unknown modifier.
470
471 switch (ExtraCode[0]) {
472 default: return true; // Unknown modifier.
473 case 'b': // Print QImode register
474 case 'h': // Print QImode high register
475 case 'w': // Print HImode register
476 case 'k': // Print SImode register
477 case 'q': // Print SImode register
478 // These only apply to registers, ignore on mem.
479 break;
480 case 'P': // Don't print @PLT, but do print as memory.
481 printMemReference(MI, OpNo, "no-rip");
482 return false;
483 }
484 }
485 printMemReference(MI, OpNo);
486 return false;
487}
488
489
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000490void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
491 if (Subtarget->isTargetDarwin()) {
492 // All darwin targets use mach-o.
493 TargetLoweringObjectFileMachO &TLOFMacho =
494 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
495
496 MachineModuleInfoMachO &MMIMacho =
497 MMI->getObjFileInfo<MachineModuleInfoMachO>();
498
499 // Output stubs for dynamically-linked functions.
500 MachineModuleInfoMachO::SymbolListTy Stubs;
501
502 Stubs = MMIMacho.GetFnStubList();
503 if (!Stubs.empty()) {
504 const MCSection *TheSection =
505 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
506 MCSectionMachO::S_SYMBOL_STUBS |
507 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
508 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
509 5, SectionKind::getMetadata());
510 OutStreamer.SwitchSection(TheSection);
511
512 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000513 O << *Stubs[i].first << ":\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000514 // Get the MCSymbol without the $stub suffix.
Chris Lattnerce409842010-01-17 21:43:43 +0000515 O << "\t.indirect_symbol " << *Stubs[i].second;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000516 O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
517 }
518 O << '\n';
519
520 Stubs.clear();
521 }
522
523 // Output stubs for external and common global variables.
524 Stubs = MMIMacho.GetGVStubList();
525 if (!Stubs.empty()) {
526 const MCSection *TheSection =
527 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
528 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
529 SectionKind::getMetadata());
530 OutStreamer.SwitchSection(TheSection);
531
532 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000533 O << *Stubs[i].first << ":\n\t.indirect_symbol " << *Stubs[i].second;
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000534 O << "\n\t.long\t0\n";
535 }
536 Stubs.clear();
537 }
538
539 Stubs = MMIMacho.GetHiddenGVStubList();
540 if (!Stubs.empty()) {
541 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
542 EmitAlignment(2);
543
544 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerce409842010-01-17 21:43:43 +0000545 O << *Stubs[i].first << ":\n" << MAI->getData32bitsDirective();
546 O << *Stubs[i].second << '\n';
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000547 }
548 Stubs.clear();
549 }
550
551 // Funny Darwin hack: This flag tells the linker that no global symbols
552 // contain code that falls through to other global symbols (e.g. the obvious
553 // implementation of multiple entry points). If this doesn't occur, the
554 // linker can safely perform dead code stripping. Since LLVM never
555 // generates code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000556 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000557 }
558
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000559 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000560 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000561 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000562
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000563 // Emit type information for external functions
564 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000565 E = COFFMMI.stub_end(); I != E; ++I) {
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000566 O << "\t.def\t " << I->getKeyData()
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000567 << ";\t.scl\t" << COFF::C_EXT
568 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
569 << ";\t.endef\n";
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000570 }
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000571
572 if (Subtarget->isTargetCygMing()) {
573 // Necessary for dllexport support
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000574 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000575
576 TargetLoweringObjectFileCOFF &TLOFCOFF =
577 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
578
579 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
580 if (I->hasDLLExportLinkage()) {
Chris Lattnerb6ed5862010-01-18 00:59:24 +0000581 MCSymbol *Sym = GetGlobalValueSymbol(I);
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000582 COFFMMI.DecorateCygMingName(Sym, OutContext, I, *TM.getTargetData());
583 DLLExportedFns.push_back(Sym);
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000584 }
585
586 for (Module::const_global_iterator I = M.global_begin(),
587 E = M.global_end(); I != E; ++I)
Chris Lattnerc53c24c2010-01-16 00:51:39 +0000588 if (I->hasDLLExportLinkage())
589 DLLExportedGlobals.push_back(GetGlobalValueSymbol(I));
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000590
591 // Output linker support code for dllexported globals on windows.
592 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
593 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
594 true,
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000595 SectionKind::getMetadata()));
Chris Lattnerce409842010-01-17 21:43:43 +0000596 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
597 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000598
Chris Lattnerce409842010-01-17 21:43:43 +0000599 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
600 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
Anton Korobeynikovd5fcb1d2009-10-15 22:36:18 +0000601 }
Chris Lattnerbf4b6a82009-09-20 07:41:30 +0000602 }
603 }
604}
605
606
607//===----------------------------------------------------------------------===//
608// Target Registry Stuff
609//===----------------------------------------------------------------------===//
610
611static MCInstPrinter *createX86MCInstPrinter(const Target &T,
612 unsigned SyntaxVariant,
613 const MCAsmInfo &MAI,
614 raw_ostream &O) {
615 if (SyntaxVariant == 0)
616 return new X86ATTInstPrinter(O, MAI);
617 if (SyntaxVariant == 1)
618 return new X86IntelInstPrinter(O, MAI);
619 return 0;
620}
621
622// Force static initialization.
623extern "C" void LLVMInitializeX86AsmPrinter() {
624 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
625 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
626
627 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
628 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
629}