blob: dcfbdb644777e5841ac9785629a64c6130faabe7 [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 Lattner88db7862010-04-04 05:19:20 +000050void X86AsmPrinter::PrintPICBaseSymbol(raw_ostream &O) 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
Chris Lattner14c38ec2010-01-28 01:02:27 +000056/// runOnMachineFunction - Emit the function body.
Chris Lattner0dc32ea2009-09-20 07:41:30 +000057///
58bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +000059 SetupMachineFunction(MF);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000060
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000061 if (Subtarget->isTargetCOFF()) {
Chris Lattnerb11caed2010-01-26 23:18:44 +000062 const Function *F = MF.getFunction();
Chris Lattner88db7862010-04-04 05:19:20 +000063 OutStreamer.EmitRawText("\t.def\t " + Twine(CurrentFnSym->getName()) +
64 ";\t.scl\t" +
65 Twine(F->hasInternalLinkage() ? COFF::C_STAT : COFF::C_EXT) +
66 ";\t.type\t" + Twine(COFF::DT_FCN << COFF::N_BTSHFT)
67 + ";\t.endef");
Chris Lattnerb11caed2010-01-26 23:18:44 +000068 }
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000069
Chris Lattnerb11caed2010-01-26 23:18:44 +000070 // Have common code print out the function header with linkage info etc.
71 EmitFunctionHeader();
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000072
Chris Lattner14c38ec2010-01-28 01:02:27 +000073 // Emit the rest of the function body.
74 EmitFunctionBody();
Chris Lattner0dc32ea2009-09-20 07:41:30 +000075
76 // We didn't modify anything.
77 return false;
78}
79
80/// printSymbolOperand - Print a raw symbol reference operand. This handles
81/// jump tables, constant pools, global address and external symbols, all of
82/// which print to a label with various suffixes for relocation types etc.
Chris Lattner88db7862010-04-04 05:19:20 +000083void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO,
84 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +000085 switch (MO.getType()) {
86 default: llvm_unreachable("unknown symbol type!");
87 case MachineOperand::MO_JumpTableIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +000088 O << *GetJTISymbol(MO.getIndex());
Chris Lattner0dc32ea2009-09-20 07:41:30 +000089 break;
90 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +000091 O << *GetCPISymbol(MO.getIndex());
Chris Lattner0c08d092010-04-03 22:28:33 +000092 printOffset(MO.getOffset(), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000093 break;
94 case MachineOperand::MO_GlobalAddress: {
95 const GlobalValue *GV = MO.getGlobal();
96
Chris Lattner5957c842010-01-18 00:59:24 +000097 MCSymbol *GVSym;
Chris Lattner0dc32ea2009-09-20 07:41:30 +000098 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattner7a2ba942010-01-16 18:37:32 +000099 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000100 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
101 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
102 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattner7a2ba942010-01-16 18:37:32 +0000103 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner4fb69f42010-01-16 00:51:39 +0000104 else
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000105 GVSym = Mang->getSymbol(GV);
Chris Lattner4fb69f42010-01-16 00:51:39 +0000106
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000107 // Handle dllimport linkage.
108 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner4fb69f42010-01-16 00:51:39 +0000109 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000110
111 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
112 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000113 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000114 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000115 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000116 if (StubSym.getPointer() == 0)
117 StubSym = MachineModuleInfoImpl::
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000118 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000119 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattner7a2ba942010-01-16 18:37:32 +0000120 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000121 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000122 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000123 if (StubSym.getPointer() == 0)
124 StubSym = MachineModuleInfoImpl::
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000125 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000126 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000127 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingcebae362010-03-10 22:34:10 +0000128 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000129 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000130 if (StubSym.getPointer() == 0)
131 StubSym = MachineModuleInfoImpl::
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000132 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000133 }
134
135 // If the name begins with a dollar-sign, enclose it in parens. We do this
136 // to avoid having it look like an integer immediate to the assembler.
Chris Lattner4fb69f42010-01-16 00:51:39 +0000137 if (GVSym->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000138 O << *GVSym;
139 else
140 O << '(' << *GVSym << ')';
Chris Lattner0c08d092010-04-03 22:28:33 +0000141 printOffset(MO.getOffset(), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000142 break;
143 }
144 case MachineOperand::MO_ExternalSymbol: {
Chris Lattneree9250b2010-01-13 07:56:59 +0000145 const MCSymbol *SymToPrint;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000146 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner8570f592010-01-16 01:00:27 +0000147 SmallString<128> TempNameStr;
148 TempNameStr += StringRef(MO.getSymbolName());
149 TempNameStr += StringRef("$stub");
150
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000151 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingcebae362010-03-10 22:34:10 +0000152 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000153 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000154 if (StubSym.getPointer() == 0) {
Chris Lattner48130352010-01-13 06:38:18 +0000155 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
Bill Wendlingcebae362010-03-10 22:34:10 +0000156 StubSym = MachineModuleInfoImpl::
157 StubValueTy(OutContext.GetOrCreateSymbol(TempNameStr.str()),
158 true);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000159 }
Bill Wendlingcebae362010-03-10 22:34:10 +0000160 SymToPrint = StubSym.getPointer();
Chris Lattneree9250b2010-01-13 07:56:59 +0000161 } else {
Chris Lattner8570f592010-01-16 01:00:27 +0000162 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000163 }
164
165 // If the name begins with a dollar-sign, enclose it in parens. We do this
166 // to avoid having it look like an integer immediate to the assembler.
Chris Lattneree9250b2010-01-13 07:56:59 +0000167 if (SymToPrint->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000168 O << *SymToPrint;
169 else
170 O << '(' << *SymToPrint << '(';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000171 break;
172 }
173 }
174
175 switch (MO.getTargetFlags()) {
176 default:
177 llvm_unreachable("Unknown target flag on GV operand");
178 case X86II::MO_NO_FLAG: // No flag.
179 break;
180 case X86II::MO_DARWIN_NONLAZY:
181 case X86II::MO_DLLIMPORT:
182 case X86II::MO_DARWIN_STUB:
183 // These affect the name of the symbol, not any suffix.
184 break;
185 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
186 O << " + [.-";
Chris Lattner88db7862010-04-04 05:19:20 +0000187 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000188 O << ']';
189 break;
190 case X86II::MO_PIC_BASE_OFFSET:
191 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
192 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
193 O << '-';
Chris Lattner88db7862010-04-04 05:19:20 +0000194 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000195 break;
196 case X86II::MO_TLSGD: O << "@TLSGD"; break;
197 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
198 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
199 case X86II::MO_TPOFF: O << "@TPOFF"; break;
200 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
201 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
202 case X86II::MO_GOT: O << "@GOT"; break;
203 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
204 case X86II::MO_PLT: O << "@PLT"; break;
205 }
206}
207
208/// print_pcrel_imm - This is used to print an immediate value that ends up
209/// being encoded as a pc-relative value. These print slightly differently, for
210/// example, a $ is not emitted.
Chris Lattner88db7862010-04-04 05:19:20 +0000211void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo,
212 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000213 const MachineOperand &MO = MI->getOperand(OpNo);
214 switch (MO.getType()) {
215 default: llvm_unreachable("Unknown pcrel immediate operand");
216 case MachineOperand::MO_Immediate:
217 O << MO.getImm();
218 return;
219 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000220 O << *MO.getMBB()->getSymbol();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000221 return;
222 case MachineOperand::MO_GlobalAddress:
223 case MachineOperand::MO_ExternalSymbol:
Chris Lattner88db7862010-04-04 05:19:20 +0000224 printSymbolOperand(MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000225 return;
226 }
227}
228
229
230void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner88db7862010-04-04 05:19:20 +0000231 raw_ostream &O, const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000232 const MachineOperand &MO = MI->getOperand(OpNo);
233 switch (MO.getType()) {
234 default: llvm_unreachable("unknown operand type!");
235 case MachineOperand::MO_Register: {
236 O << '%';
237 unsigned Reg = MO.getReg();
238 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
239 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
240 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
241 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
242 Reg = getX86SubSuperRegister(Reg, VT);
243 }
244 O << X86ATTInstPrinter::getRegisterName(Reg);
245 return;
246 }
247
248 case MachineOperand::MO_Immediate:
249 O << '$' << MO.getImm();
250 return;
251
252 case MachineOperand::MO_JumpTableIndex:
253 case MachineOperand::MO_ConstantPoolIndex:
254 case MachineOperand::MO_GlobalAddress:
255 case MachineOperand::MO_ExternalSymbol: {
256 O << '$';
Chris Lattner88db7862010-04-04 05:19:20 +0000257 printSymbolOperand(MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000258 break;
259 }
260 }
261}
262
Chris Lattner88db7862010-04-04 05:19:20 +0000263void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
264 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000265 unsigned char value = MI->getOperand(Op).getImm();
266 assert(value <= 7 && "Invalid ssecc argument!");
267 switch (value) {
268 case 0: O << "eq"; break;
269 case 1: O << "lt"; break;
270 case 2: O << "le"; break;
271 case 3: O << "unord"; break;
272 case 4: O << "neq"; break;
273 case 5: O << "nlt"; break;
274 case 6: O << "nle"; break;
275 case 7: O << "ord"; break;
276 }
277}
278
279void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner88db7862010-04-04 05:19:20 +0000280 raw_ostream &O, const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000281 const MachineOperand &BaseReg = MI->getOperand(Op);
282 const MachineOperand &IndexReg = MI->getOperand(Op+2);
283 const MachineOperand &DispSpec = MI->getOperand(Op+3);
284
285 // If we really don't want to print out (rip), don't.
286 bool HasBaseReg = BaseReg.getReg() != 0;
287 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
288 BaseReg.getReg() == X86::RIP)
289 HasBaseReg = false;
290
291 // HasParenPart - True if we will print out the () part of the mem ref.
292 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
293
294 if (DispSpec.isImm()) {
295 int DispVal = DispSpec.getImm();
296 if (DispVal || !HasParenPart)
297 O << DispVal;
298 } else {
299 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
300 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner88db7862010-04-04 05:19:20 +0000301 printSymbolOperand(MI->getOperand(Op+3), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000302 }
303
304 if (HasParenPart) {
305 assert(IndexReg.getReg() != X86::ESP &&
306 "X86 doesn't allow scaling by ESP");
307
308 O << '(';
309 if (HasBaseReg)
Chris Lattner88db7862010-04-04 05:19:20 +0000310 printOperand(MI, Op, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000311
312 if (IndexReg.getReg()) {
313 O << ',';
Chris Lattner88db7862010-04-04 05:19:20 +0000314 printOperand(MI, Op+2, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000315 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
316 if (ScaleVal != 1)
317 O << ',' << ScaleVal;
318 }
319 O << ')';
320 }
321}
322
323void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner88db7862010-04-04 05:19:20 +0000324 raw_ostream &O, const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000325 assert(isMem(MI, Op) && "Invalid memory reference!");
326 const MachineOperand &Segment = MI->getOperand(Op+4);
327 if (Segment.getReg()) {
Chris Lattner88db7862010-04-04 05:19:20 +0000328 printOperand(MI, Op+4, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000329 O << ':';
330 }
Chris Lattner88db7862010-04-04 05:19:20 +0000331 printLeaMemReference(MI, Op, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000332}
333
Chris Lattner88db7862010-04-04 05:19:20 +0000334void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op,
335 raw_ostream &O) {
336 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000337 O << '\n';
Chris Lattner88db7862010-04-04 05:19:20 +0000338 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000339 O << ':';
340}
341
Chris Lattner88db7862010-04-04 05:19:20 +0000342bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode,
343 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000344 unsigned Reg = MO.getReg();
345 switch (Mode) {
346 default: return true; // Unknown mode.
347 case 'b': // Print QImode register
348 Reg = getX86SubSuperRegister(Reg, MVT::i8);
349 break;
350 case 'h': // Print QImode high register
351 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
352 break;
353 case 'w': // Print HImode register
354 Reg = getX86SubSuperRegister(Reg, MVT::i16);
355 break;
356 case 'k': // Print SImode register
357 Reg = getX86SubSuperRegister(Reg, MVT::i32);
358 break;
359 case 'q': // Print DImode register
360 Reg = getX86SubSuperRegister(Reg, MVT::i64);
361 break;
362 }
363
364 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
365 return false;
366}
367
368/// PrintAsmOperand - Print out an operand for an inline asm expression.
369///
370bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
371 unsigned AsmVariant,
372 const char *ExtraCode) {
373 // Does this asm operand have a single letter operand modifier?
374 if (ExtraCode && ExtraCode[0]) {
375 if (ExtraCode[1] != 0) return true; // Unknown modifier.
376
377 const MachineOperand &MO = MI->getOperand(OpNo);
378
379 switch (ExtraCode[0]) {
380 default: return true; // Unknown modifier.
381 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
382 if (MO.isImm()) {
383 O << MO.getImm();
384 return false;
385 }
386 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
Chris Lattner88db7862010-04-04 05:19:20 +0000387 printSymbolOperand(MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000388 return false;
389 }
390 if (MO.isReg()) {
391 O << '(';
Chris Lattner88db7862010-04-04 05:19:20 +0000392 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000393 O << ')';
394 return false;
395 }
396 return true;
397
398 case 'c': // Don't print "$" before a global var name or constant.
399 if (MO.isImm())
400 O << MO.getImm();
401 else if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol())
Chris Lattner88db7862010-04-04 05:19:20 +0000402 printSymbolOperand(MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000403 else
Chris Lattner88db7862010-04-04 05:19:20 +0000404 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000405 return false;
406
407 case 'A': // Print '*' before a register (it must be a register)
408 if (MO.isReg()) {
409 O << '*';
Chris Lattner88db7862010-04-04 05:19:20 +0000410 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000411 return false;
412 }
413 return true;
414
415 case 'b': // Print QImode register
416 case 'h': // Print QImode high register
417 case 'w': // Print HImode register
418 case 'k': // Print SImode register
419 case 'q': // Print DImode register
420 if (MO.isReg())
Chris Lattner88db7862010-04-04 05:19:20 +0000421 return printAsmMRegister(MO, ExtraCode[0], O);
422 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000423 return false;
424
425 case 'P': // This is the operand of a call, treat specially.
Chris Lattner88db7862010-04-04 05:19:20 +0000426 print_pcrel_imm(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000427 return false;
428
429 case 'n': // Negate the immediate or print a '-' before the operand.
430 // Note: this is a temporary solution. It should be handled target
431 // independently as part of the 'MC' work.
432 if (MO.isImm()) {
433 O << -MO.getImm();
434 return false;
435 }
436 O << '-';
437 }
438 }
439
Chris Lattner88db7862010-04-04 05:19:20 +0000440 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000441 return false;
442}
443
444bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
445 unsigned OpNo, unsigned AsmVariant,
446 const char *ExtraCode) {
447 if (ExtraCode && ExtraCode[0]) {
448 if (ExtraCode[1] != 0) return true; // Unknown modifier.
449
450 switch (ExtraCode[0]) {
451 default: return true; // Unknown modifier.
452 case 'b': // Print QImode register
453 case 'h': // Print QImode high register
454 case 'w': // Print HImode register
455 case 'k': // Print SImode register
456 case 'q': // Print SImode register
457 // These only apply to registers, ignore on mem.
458 break;
459 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner88db7862010-04-04 05:19:20 +0000460 printMemReference(MI, OpNo, O, "no-rip");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000461 return false;
462 }
463 }
Chris Lattner88db7862010-04-04 05:19:20 +0000464 printMemReference(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000465 return false;
466}
467
Chris Lattner1bd1e6d2010-03-13 02:10:00 +0000468void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
469 if (Subtarget->isTargetDarwin())
470 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
471}
472
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000473
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000474void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
475 if (Subtarget->isTargetDarwin()) {
476 // All darwin targets use mach-o.
477 TargetLoweringObjectFileMachO &TLOFMacho =
478 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
479
480 MachineModuleInfoMachO &MMIMacho =
481 MMI->getObjFileInfo<MachineModuleInfoMachO>();
482
483 // Output stubs for dynamically-linked functions.
484 MachineModuleInfoMachO::SymbolListTy Stubs;
485
486 Stubs = MMIMacho.GetFnStubList();
487 if (!Stubs.empty()) {
488 const MCSection *TheSection =
489 TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
490 MCSectionMachO::S_SYMBOL_STUBS |
491 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
492 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
493 5, SectionKind::getMetadata());
494 OutStreamer.SwitchSection(TheSection);
495
496 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000497 // L_foo$stub:
498 OutStreamer.EmitLabel(Stubs[i].first);
499 // .indirect_symbol _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000500 OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
501 MCSA_IndirectSymbol);
Chris Lattnerbeb42692010-02-03 06:42:38 +0000502 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4 = -12.
503 const char HltInsts[] = { -12, -12, -12, -12, -12 };
504 OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000505 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000506
507 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000508 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000509 }
510
511 // Output stubs for external and common global variables.
512 Stubs = MMIMacho.GetGVStubList();
513 if (!Stubs.empty()) {
514 const MCSection *TheSection =
515 TLOFMacho.getMachOSection("__IMPORT", "__pointers",
516 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
517 SectionKind::getMetadata());
518 OutStreamer.SwitchSection(TheSection);
519
520 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000521 // L_foo$non_lazy_ptr:
522 OutStreamer.EmitLabel(Stubs[i].first);
523 // .indirect_symbol _foo
Bill Wendlingec041eb2010-03-12 19:20:40 +0000524 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
525 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),
Bill Wendlingcebae362010-03-10 22:34:10 +0000526 MCSA_IndirectSymbol);
Chris Lattnerbeb42692010-02-03 06:42:38 +0000527 // .long 0
Bill Wendlingec041eb2010-03-12 19:20:40 +0000528 if (MCSym.getInt())
529 // External to current translation unit.
530 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
531 else
532 // Internal to current translation unit.
Bill Wendlingd93bf042010-03-31 18:48:58 +0000533 //
534 // When we place the LSDA into the TEXT section, the type info
535 // pointers need to be indirect and pc-rel. We accomplish this by
536 // using NLPs. However, sometimes the types are local to the file. So
537 // we need to fill in the value for the NLP in those cases.
Bill Wendlingec041eb2010-03-12 19:20:40 +0000538 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
539 OutContext),
540 4/*size*/, 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000541 }
542 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000543 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000544 }
545
546 Stubs = MMIMacho.GetHiddenGVStubList();
547 if (!Stubs.empty()) {
548 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
549 EmitAlignment(2);
550
551 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000552 // L_foo$non_lazy_ptr:
553 OutStreamer.EmitLabel(Stubs[i].first);
554 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000555 OutStreamer.EmitValue(MCSymbolRefExpr::
556 Create(Stubs[i].second.getPointer(),
557 OutContext),
Chris Lattnerbeb42692010-02-03 06:42:38 +0000558 4/*size*/, 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000559 }
560 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000561 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000562 }
563
564 // Funny Darwin hack: This flag tells the linker that no global symbols
565 // contain code that falls through to other global symbols (e.g. the obvious
566 // implementation of multiple entry points). If this doesn't occur, the
567 // linker can safely perform dead code stripping. Since LLVM never
568 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000569 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000570 }
571
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000572 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000573 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000574 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000575
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000576 // Emit type information for external functions
577 for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000578 E = COFFMMI.stub_end(); I != E; ++I) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000579 O << "\t.def\t " << I->getKeyData()
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000580 << ";\t.scl\t" << COFF::C_EXT
581 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
582 << ";\t.endef\n";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000583 }
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000584
585 if (Subtarget->isTargetCygMing()) {
586 // Necessary for dllexport support
Chris Lattner4fb69f42010-01-16 00:51:39 +0000587 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000588
589 TargetLoweringObjectFileCOFF &TLOFCOFF =
590 static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
591
592 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000593 if (I->hasDLLExportLinkage())
594 DLLExportedFns.push_back(Mang->getSymbol(I));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000595
596 for (Module::const_global_iterator I = M.global_begin(),
597 E = M.global_end(); I != E; ++I)
Chris Lattner4fb69f42010-01-16 00:51:39 +0000598 if (I->hasDLLExportLinkage())
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000599 DLLExportedGlobals.push_back(Mang->getSymbol(I));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000600
601 // Output linker support code for dllexported globals on windows.
602 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
603 OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
604 true,
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000605 SectionKind::getMetadata()));
Chris Lattner10b318b2010-01-17 21:43:43 +0000606 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
607 O << "\t.ascii \" -export:" << *DLLExportedGlobals[i] << ",data\"\n";
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000608
Chris Lattner10b318b2010-01-17 21:43:43 +0000609 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
610 O << "\t.ascii \" -export:" << *DLLExportedFns[i] << "\"\n";
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000611 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000612 }
613 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000614
615 if (Subtarget->isTargetELF()) {
616 TargetLoweringObjectFileELF &TLOFELF =
617 static_cast<TargetLoweringObjectFileELF &>(getObjFileLowering());
618
619 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
620
621 // Output stubs for external and common global variables.
622 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
623 if (!Stubs.empty()) {
624 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
625 const TargetData *TD = TM.getTargetData();
626
627 for (unsigned i = 0, e = Stubs.size(); i != e; ++i)
628 O << *Stubs[i].first << ":\n"
629 << (TD->getPointerSize() == 8 ?
630 MAI->getData64bitsDirective() : MAI->getData32bitsDirective())
Bill Wendlingcebae362010-03-10 22:34:10 +0000631 << *Stubs[i].second.getPointer() << '\n';
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000632
633 Stubs.clear();
634 }
635 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000636}
637
638
639//===----------------------------------------------------------------------===//
640// Target Registry Stuff
641//===----------------------------------------------------------------------===//
642
643static MCInstPrinter *createX86MCInstPrinter(const Target &T,
644 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +0000645 const MCAsmInfo &MAI) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000646 if (SyntaxVariant == 0)
Chris Lattnerd3740872010-04-04 05:04:31 +0000647 return new X86ATTInstPrinter(MAI);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000648 if (SyntaxVariant == 1)
Chris Lattnerd3740872010-04-04 05:04:31 +0000649 return new X86IntelInstPrinter(MAI);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000650 return 0;
651}
652
653// Force static initialization.
654extern "C" void LLVMInitializeX86AsmPrinter() {
655 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
656 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
657
658 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
659 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
660}