blob: 3e9fbb09c91d61657ff1bdc80a502de6e3248005 [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"
Chris Lattneraef40352010-07-19 23:41:57 +000016#include "AsmPrinter/X86ATTInstPrinter.h"
17#include "AsmPrinter/X86IntelInstPrinter.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000018#include "X86MCInstLower.h"
19#include "X86.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000020#include "X86COFFMachineModuleInfo.h"
21#include "X86MachineFunctionInfo.h"
22#include "X86TargetMachine.h"
23#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Module.h"
26#include "llvm/Type.h"
Chris Lattner1a34c832010-07-20 22:18:19 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000028#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 Lattner9fe2c672010-07-14 18:14:33 +000038#include "llvm/Support/COFF.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000039#include "llvm/Support/ErrorHandling.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 Lattnerb54b9dd2010-05-08 19:54:22 +000062 bool Intrn = MF.getFunction()->hasInternalLinkage();
63 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
Chris Lattner9fe2c672010-07-14 18:14:33 +000064 OutStreamer.EmitCOFFSymbolStorageClass(Intrn ? COFF::IMAGE_SYM_CLASS_STATIC
65 : COFF::IMAGE_SYM_CLASS_EXTERNAL);
66 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
67 << COFF::SCT_COMPLEX_TYPE_SHIFT);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000068 OutStreamer.EndCOFFSymbolDef();
Chris Lattnerb11caed2010-01-26 23:18:44 +000069 }
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000070
Chris Lattnerb11caed2010-01-26 23:18:44 +000071 // Have common code print out the function header with linkage info etc.
72 EmitFunctionHeader();
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000073
Chris Lattner14c38ec2010-01-28 01:02:27 +000074 // Emit the rest of the function body.
75 EmitFunctionBody();
Chris Lattner0dc32ea2009-09-20 07:41:30 +000076
77 // We didn't modify anything.
78 return false;
79}
80
81/// printSymbolOperand - Print a raw symbol reference operand. This handles
82/// jump tables, constant pools, global address and external symbols, all of
83/// which print to a label with various suffixes for relocation types etc.
Chris Lattner88db7862010-04-04 05:19:20 +000084void X86AsmPrinter::printSymbolOperand(const MachineOperand &MO,
85 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +000086 switch (MO.getType()) {
87 default: llvm_unreachable("unknown symbol type!");
88 case MachineOperand::MO_JumpTableIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +000089 O << *GetJTISymbol(MO.getIndex());
Chris Lattner0dc32ea2009-09-20 07:41:30 +000090 break;
91 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner64c2b242010-01-23 05:26:25 +000092 O << *GetCPISymbol(MO.getIndex());
Chris Lattner0c08d092010-04-03 22:28:33 +000093 printOffset(MO.getOffset(), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000094 break;
95 case MachineOperand::MO_GlobalAddress: {
96 const GlobalValue *GV = MO.getGlobal();
97
Chris Lattner5957c842010-01-18 00:59:24 +000098 MCSymbol *GVSym;
Chris Lattner0dc32ea2009-09-20 07:41:30 +000099 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Chris Lattner7a2ba942010-01-16 18:37:32 +0000100 GVSym = GetSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000101 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
102 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
103 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Chris Lattner7a2ba942010-01-16 18:37:32 +0000104 GVSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner4fb69f42010-01-16 00:51:39 +0000105 else
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000106 GVSym = Mang->getSymbol(GV);
Chris Lattner4fb69f42010-01-16 00:51:39 +0000107
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000108 // Handle dllimport linkage.
109 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner4fb69f42010-01-16 00:51:39 +0000110 GVSym = OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000111
112 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
113 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000114 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000115 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000116 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000117 if (StubSym.getPointer() == 0)
118 StubSym = MachineModuleInfoImpl::
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000119 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000120 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Chris Lattner7a2ba942010-01-16 18:37:32 +0000121 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000122 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000123 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000124 if (StubSym.getPointer() == 0)
125 StubSym = MachineModuleInfoImpl::
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000126 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000127 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000128 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingcebae362010-03-10 22:34:10 +0000129 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000130 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000131 if (StubSym.getPointer() == 0)
132 StubSym = MachineModuleInfoImpl::
Chris Lattnerdeb0cba2010-03-12 21:09:07 +0000133 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000134 }
135
136 // If the name begins with a dollar-sign, enclose it in parens. We do this
137 // to avoid having it look like an integer immediate to the assembler.
Chris Lattner4fb69f42010-01-16 00:51:39 +0000138 if (GVSym->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000139 O << *GVSym;
140 else
141 O << '(' << *GVSym << ')';
Chris Lattner0c08d092010-04-03 22:28:33 +0000142 printOffset(MO.getOffset(), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000143 break;
144 }
145 case MachineOperand::MO_ExternalSymbol: {
Chris Lattneree9250b2010-01-13 07:56:59 +0000146 const MCSymbol *SymToPrint;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000147 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Chris Lattner8570f592010-01-16 01:00:27 +0000148 SmallString<128> TempNameStr;
149 TempNameStr += StringRef(MO.getSymbolName());
150 TempNameStr += StringRef("$stub");
151
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000152 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingcebae362010-03-10 22:34:10 +0000153 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000154 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000155 if (StubSym.getPointer() == 0) {
Chris Lattner48130352010-01-13 06:38:18 +0000156 TempNameStr.erase(TempNameStr.end()-5, TempNameStr.end());
Bill Wendlingcebae362010-03-10 22:34:10 +0000157 StubSym = MachineModuleInfoImpl::
158 StubValueTy(OutContext.GetOrCreateSymbol(TempNameStr.str()),
159 true);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000160 }
Bill Wendlingcebae362010-03-10 22:34:10 +0000161 SymToPrint = StubSym.getPointer();
Chris Lattneree9250b2010-01-13 07:56:59 +0000162 } else {
Chris Lattner8570f592010-01-16 01:00:27 +0000163 SymToPrint = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000164 }
165
166 // If the name begins with a dollar-sign, enclose it in parens. We do this
167 // to avoid having it look like an integer immediate to the assembler.
Chris Lattneree9250b2010-01-13 07:56:59 +0000168 if (SymToPrint->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000169 O << *SymToPrint;
170 else
171 O << '(' << *SymToPrint << '(';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000172 break;
173 }
174 }
175
176 switch (MO.getTargetFlags()) {
177 default:
178 llvm_unreachable("Unknown target flag on GV operand");
179 case X86II::MO_NO_FLAG: // No flag.
180 break;
181 case X86II::MO_DARWIN_NONLAZY:
182 case X86II::MO_DLLIMPORT:
183 case X86II::MO_DARWIN_STUB:
184 // These affect the name of the symbol, not any suffix.
185 break;
186 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
187 O << " + [.-";
Chris Lattner88db7862010-04-04 05:19:20 +0000188 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000189 O << ']';
190 break;
191 case X86II::MO_PIC_BASE_OFFSET:
192 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
193 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
194 O << '-';
Chris Lattner88db7862010-04-04 05:19:20 +0000195 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000196 break;
197 case X86II::MO_TLSGD: O << "@TLSGD"; break;
198 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
199 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
200 case X86II::MO_TPOFF: O << "@TPOFF"; break;
201 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
202 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
203 case X86II::MO_GOT: O << "@GOT"; break;
204 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
205 case X86II::MO_PLT: O << "@PLT"; break;
Eric Christopher30ef0e52010-06-03 04:07:48 +0000206 case X86II::MO_TLVP: O << "@TLVP"; break;
207 case X86II::MO_TLVP_PIC_BASE:
208 O << "@TLVP" << '-';
209 PrintPICBaseSymbol(O);
210 break;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000211 }
212}
213
214/// print_pcrel_imm - This is used to print an immediate value that ends up
215/// being encoded as a pc-relative value. These print slightly differently, for
216/// example, a $ is not emitted.
Chris Lattner88db7862010-04-04 05:19:20 +0000217void X86AsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo,
218 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000219 const MachineOperand &MO = MI->getOperand(OpNo);
220 switch (MO.getType()) {
221 default: llvm_unreachable("Unknown pcrel immediate operand");
Dale Johannesen323200d2010-07-16 18:35:46 +0000222 case MachineOperand::MO_Register:
223 // pc-relativeness was handled when computing the value in the reg.
224 printOperand(MI, OpNo, O);
225 return;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000226 case MachineOperand::MO_Immediate:
227 O << MO.getImm();
228 return;
229 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000230 O << *MO.getMBB()->getSymbol();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000231 return;
232 case MachineOperand::MO_GlobalAddress:
233 case MachineOperand::MO_ExternalSymbol:
Chris Lattner88db7862010-04-04 05:19:20 +0000234 printSymbolOperand(MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000235 return;
236 }
237}
238
239
240void X86AsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner88db7862010-04-04 05:19:20 +0000241 raw_ostream &O, const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000242 const MachineOperand &MO = MI->getOperand(OpNo);
243 switch (MO.getType()) {
244 default: llvm_unreachable("unknown operand type!");
245 case MachineOperand::MO_Register: {
246 O << '%';
247 unsigned Reg = MO.getReg();
248 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
249 EVT VT = (strcmp(Modifier+6,"64") == 0) ?
250 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
251 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
252 Reg = getX86SubSuperRegister(Reg, VT);
253 }
254 O << X86ATTInstPrinter::getRegisterName(Reg);
255 return;
256 }
257
258 case MachineOperand::MO_Immediate:
259 O << '$' << MO.getImm();
260 return;
261
262 case MachineOperand::MO_JumpTableIndex:
263 case MachineOperand::MO_ConstantPoolIndex:
264 case MachineOperand::MO_GlobalAddress:
265 case MachineOperand::MO_ExternalSymbol: {
266 O << '$';
Chris Lattner88db7862010-04-04 05:19:20 +0000267 printSymbolOperand(MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000268 break;
269 }
270 }
271}
272
Chris Lattner88db7862010-04-04 05:19:20 +0000273void X86AsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
274 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000275 unsigned char value = MI->getOperand(Op).getImm();
276 assert(value <= 7 && "Invalid ssecc argument!");
277 switch (value) {
278 case 0: O << "eq"; break;
279 case 1: O << "lt"; break;
280 case 2: O << "le"; break;
281 case 3: O << "unord"; break;
282 case 4: O << "neq"; break;
283 case 5: O << "nlt"; break;
284 case 6: O << "nle"; break;
285 case 7: O << "ord"; break;
286 }
287}
288
289void X86AsmPrinter::printLeaMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner88db7862010-04-04 05:19:20 +0000290 raw_ostream &O, const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000291 const MachineOperand &BaseReg = MI->getOperand(Op);
292 const MachineOperand &IndexReg = MI->getOperand(Op+2);
293 const MachineOperand &DispSpec = MI->getOperand(Op+3);
294
295 // If we really don't want to print out (rip), don't.
296 bool HasBaseReg = BaseReg.getReg() != 0;
297 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
298 BaseReg.getReg() == X86::RIP)
299 HasBaseReg = false;
300
301 // HasParenPart - True if we will print out the () part of the mem ref.
302 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
303
304 if (DispSpec.isImm()) {
305 int DispVal = DispSpec.getImm();
306 if (DispVal || !HasParenPart)
307 O << DispVal;
308 } else {
309 assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
310 DispSpec.isJTI() || DispSpec.isSymbol());
Chris Lattner88db7862010-04-04 05:19:20 +0000311 printSymbolOperand(MI->getOperand(Op+3), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000312 }
313
314 if (HasParenPart) {
315 assert(IndexReg.getReg() != X86::ESP &&
316 "X86 doesn't allow scaling by ESP");
317
318 O << '(';
319 if (HasBaseReg)
Chris Lattner88db7862010-04-04 05:19:20 +0000320 printOperand(MI, Op, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000321
322 if (IndexReg.getReg()) {
323 O << ',';
Chris Lattner88db7862010-04-04 05:19:20 +0000324 printOperand(MI, Op+2, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000325 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
326 if (ScaleVal != 1)
327 O << ',' << ScaleVal;
328 }
329 O << ')';
330 }
331}
332
333void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
Chris Lattner88db7862010-04-04 05:19:20 +0000334 raw_ostream &O, const char *Modifier) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000335 assert(isMem(MI, Op) && "Invalid memory reference!");
336 const MachineOperand &Segment = MI->getOperand(Op+4);
337 if (Segment.getReg()) {
Chris Lattner88db7862010-04-04 05:19:20 +0000338 printOperand(MI, Op+4, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000339 O << ':';
340 }
Chris Lattner88db7862010-04-04 05:19:20 +0000341 printLeaMemReference(MI, Op, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000342}
343
Chris Lattner88db7862010-04-04 05:19:20 +0000344void X86AsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op,
345 raw_ostream &O) {
346 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000347 O << '\n';
Chris Lattner88db7862010-04-04 05:19:20 +0000348 PrintPICBaseSymbol(O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000349 O << ':';
350}
351
Chris Lattner88db7862010-04-04 05:19:20 +0000352bool X86AsmPrinter::printAsmMRegister(const MachineOperand &MO, char Mode,
353 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000354 unsigned Reg = MO.getReg();
355 switch (Mode) {
356 default: return true; // Unknown mode.
357 case 'b': // Print QImode register
358 Reg = getX86SubSuperRegister(Reg, MVT::i8);
359 break;
360 case 'h': // Print QImode high register
361 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
362 break;
363 case 'w': // Print HImode register
364 Reg = getX86SubSuperRegister(Reg, MVT::i16);
365 break;
366 case 'k': // Print SImode register
367 Reg = getX86SubSuperRegister(Reg, MVT::i32);
368 break;
369 case 'q': // Print DImode register
370 Reg = getX86SubSuperRegister(Reg, MVT::i64);
371 break;
372 }
373
374 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
375 return false;
376}
377
378/// PrintAsmOperand - Print out an operand for an inline asm expression.
379///
380bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
381 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000382 const char *ExtraCode, raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000383 // Does this asm operand have a single letter operand modifier?
384 if (ExtraCode && ExtraCode[0]) {
385 if (ExtraCode[1] != 0) return true; // Unknown modifier.
386
387 const MachineOperand &MO = MI->getOperand(OpNo);
388
389 switch (ExtraCode[0]) {
390 default: return true; // Unknown modifier.
391 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
392 if (MO.isImm()) {
393 O << MO.getImm();
394 return false;
395 }
396 if (MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isSymbol()) {
Chris Lattner88db7862010-04-04 05:19:20 +0000397 printSymbolOperand(MO, O);
Dale Johannesene2b448c2010-07-06 23:27:00 +0000398 if (Subtarget->isPICStyleRIPRel())
399 O << "(%rip)";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000400 return false;
401 }
402 if (MO.isReg()) {
403 O << '(';
Chris Lattner88db7862010-04-04 05:19:20 +0000404 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000405 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())
Chris Lattner88db7862010-04-04 05:19:20 +0000414 printSymbolOperand(MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000415 else
Chris Lattner88db7862010-04-04 05:19:20 +0000416 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000417 return false;
418
419 case 'A': // Print '*' before a register (it must be a register)
420 if (MO.isReg()) {
421 O << '*';
Chris Lattner88db7862010-04-04 05:19:20 +0000422 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000423 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())
Chris Lattner88db7862010-04-04 05:19:20 +0000433 return printAsmMRegister(MO, ExtraCode[0], O);
434 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000435 return false;
436
437 case 'P': // This is the operand of a call, treat specially.
Chris Lattner88db7862010-04-04 05:19:20 +0000438 print_pcrel_imm(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000439 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
Chris Lattner88db7862010-04-04 05:19:20 +0000452 printOperand(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000453 return false;
454}
455
456bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
457 unsigned OpNo, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000458 const char *ExtraCode,
459 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000460 if (ExtraCode && ExtraCode[0]) {
461 if (ExtraCode[1] != 0) return true; // Unknown modifier.
462
463 switch (ExtraCode[0]) {
464 default: return true; // Unknown modifier.
465 case 'b': // Print QImode register
466 case 'h': // Print QImode high register
467 case 'w': // Print HImode register
468 case 'k': // Print SImode register
469 case 'q': // Print SImode register
470 // These only apply to registers, ignore on mem.
471 break;
472 case 'P': // Don't print @PLT, but do print as memory.
Chris Lattner88db7862010-04-04 05:19:20 +0000473 printMemReference(MI, OpNo, O, "no-rip");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000474 return false;
475 }
476 }
Chris Lattner88db7862010-04-04 05:19:20 +0000477 printMemReference(MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000478 return false;
479}
480
Chris Lattner1bd1e6d2010-03-13 02:10:00 +0000481void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
482 if (Subtarget->isTargetDarwin())
483 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
484}
485
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000486
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000487void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
488 if (Subtarget->isTargetDarwin()) {
489 // All darwin targets use mach-o.
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000490 MachineModuleInfoMachO &MMIMacho =
491 MMI->getObjFileInfo<MachineModuleInfoMachO>();
492
493 // Output stubs for dynamically-linked functions.
494 MachineModuleInfoMachO::SymbolListTy Stubs;
495
496 Stubs = MMIMacho.GetFnStubList();
497 if (!Stubs.empty()) {
498 const MCSection *TheSection =
Chris Lattner22772212010-04-08 20:40:11 +0000499 OutContext.getMachOSection("__IMPORT", "__jump_table",
500 MCSectionMachO::S_SYMBOL_STUBS |
501 MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
502 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
503 5, SectionKind::getMetadata());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000504 OutStreamer.SwitchSection(TheSection);
505
506 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000507 // L_foo$stub:
508 OutStreamer.EmitLabel(Stubs[i].first);
509 // .indirect_symbol _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000510 OutStreamer.EmitSymbolAttribute(Stubs[i].second.getPointer(),
511 MCSA_IndirectSymbol);
Chris Lattnerbeb42692010-02-03 06:42:38 +0000512 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4 = -12.
513 const char HltInsts[] = { -12, -12, -12, -12, -12 };
514 OutStreamer.EmitBytes(StringRef(HltInsts, 5), 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000515 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000516
517 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000518 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000519 }
520
521 // Output stubs for external and common global variables.
522 Stubs = MMIMacho.GetGVStubList();
523 if (!Stubs.empty()) {
524 const MCSection *TheSection =
Chris Lattner22772212010-04-08 20:40:11 +0000525 OutContext.getMachOSection("__IMPORT", "__pointers",
526 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
527 SectionKind::getMetadata());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000528 OutStreamer.SwitchSection(TheSection);
529
530 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000531 // L_foo$non_lazy_ptr:
532 OutStreamer.EmitLabel(Stubs[i].first);
533 // .indirect_symbol _foo
Bill Wendlingec041eb2010-03-12 19:20:40 +0000534 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
535 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),
Bill Wendlingcebae362010-03-10 22:34:10 +0000536 MCSA_IndirectSymbol);
Chris Lattnerbeb42692010-02-03 06:42:38 +0000537 // .long 0
Bill Wendlingec041eb2010-03-12 19:20:40 +0000538 if (MCSym.getInt())
539 // External to current translation unit.
540 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
541 else
542 // Internal to current translation unit.
Bill Wendlingd93bf042010-03-31 18:48:58 +0000543 //
544 // When we place the LSDA into the TEXT section, the type info
545 // pointers need to be indirect and pc-rel. We accomplish this by
546 // using NLPs. However, sometimes the types are local to the file. So
547 // we need to fill in the value for the NLP in those cases.
Bill Wendlingec041eb2010-03-12 19:20:40 +0000548 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
549 OutContext),
550 4/*size*/, 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000551 }
552 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000553 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000554 }
555
556 Stubs = MMIMacho.GetHiddenGVStubList();
557 if (!Stubs.empty()) {
558 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
559 EmitAlignment(2);
560
561 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000562 // L_foo$non_lazy_ptr:
563 OutStreamer.EmitLabel(Stubs[i].first);
564 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000565 OutStreamer.EmitValue(MCSymbolRefExpr::
566 Create(Stubs[i].second.getPointer(),
567 OutContext),
Chris Lattnerbeb42692010-02-03 06:42:38 +0000568 4/*size*/, 0/*addrspace*/);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000569 }
570 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000571 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000572 }
573
574 // Funny Darwin hack: This flag tells the linker that no global symbols
575 // contain code that falls through to other global symbols (e.g. the obvious
576 // implementation of multiple entry points). If this doesn't occur, the
577 // linker can safely perform dead code stripping. Since LLVM never
578 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000579 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000580 }
581
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000582 if (Subtarget->isTargetCOFF()) {
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000583 X86COFFMachineModuleInfo &COFFMMI =
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000584 MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000585
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000586 // Emit type information for external functions
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000587 typedef X86COFFMachineModuleInfo::externals_iterator externals_iterator;
588 for (externals_iterator I = COFFMMI.externals_begin(),
589 E = COFFMMI.externals_end();
590 I != E; ++I) {
591 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
Chris Lattner9fe2c672010-07-14 18:14:33 +0000592 OutStreamer.EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_EXTERNAL);
593 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
594 << COFF::SCT_COMPLEX_TYPE_SHIFT);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +0000595 OutStreamer.EndCOFFSymbolDef();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000596 }
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000597
Nathan Jeffords071de922010-05-09 05:52:28 +0000598 // Necessary for dllexport support
599 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000600
Nathan Jeffords071de922010-05-09 05:52:28 +0000601 const TargetLoweringObjectFileCOFF &TLOFCOFF =
602 static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000603
Nathan Jeffords071de922010-05-09 05:52:28 +0000604 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
605 if (I->hasDLLExportLinkage())
606 DLLExportedFns.push_back(Mang->getSymbol(I));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000607
Nathan Jeffords071de922010-05-09 05:52:28 +0000608 for (Module::const_global_iterator I = M.global_begin(),
609 E = M.global_end(); I != E; ++I)
610 if (I->hasDLLExportLinkage())
611 DLLExportedGlobals.push_back(Mang->getSymbol(I));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000612
Nathan Jeffords071de922010-05-09 05:52:28 +0000613 // Output linker support code for dllexported globals on windows.
614 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
615 OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
Nathan Jeffordsbb597322010-05-09 08:40:06 +0000616 SmallString<128> name;
617 for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i) {
618 if (Subtarget->isTargetWindows())
619 name = " /EXPORT:";
620 else
621 name = " -export:";
622 name += DLLExportedGlobals[i]->getName();
623 if (Subtarget->isTargetWindows())
624 name += ",DATA";
625 else
626 name += ",data";
627 OutStreamer.EmitBytes(name, 0);
628 }
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000629
Nathan Jeffordsbb597322010-05-09 08:40:06 +0000630 for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i) {
631 if (Subtarget->isTargetWindows())
632 name = " /EXPORT:";
633 else
634 name = " -export:";
635 name += DLLExportedFns[i]->getName();
636 OutStreamer.EmitBytes(name, 0);
637 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000638 }
639 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000640
641 if (Subtarget->isTargetELF()) {
Dan Gohman0d805c32010-04-17 16:44:48 +0000642 const TargetLoweringObjectFileELF &TLOFELF =
643 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000644
645 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
646
647 // Output stubs for external and common global variables.
648 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
649 if (!Stubs.empty()) {
650 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
651 const TargetData *TD = TM.getTargetData();
652
Chris Lattnerc82d9c42010-04-04 05:35:04 +0000653 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
654 OutStreamer.EmitLabel(Stubs[i].first);
655 OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(),
656 TD->getPointerSize(), 0);
657 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000658 Stubs.clear();
659 }
660 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000661}
662
Chris Lattner1a34c832010-07-20 22:18:19 +0000663MachineLocation
664X86AsmPrinter::getDebugValueLocation(const MachineInstr *MI) const {
665 MachineLocation Location;
666 assert (MI->getNumOperands() == 7 && "Invalid no. of machine operands!");
667 // Frame address. Currently handles register +- offset only.
668
669 if (MI->getOperand(0).isReg() && MI->getOperand(3).isImm())
670 Location.set(MI->getOperand(0).getReg(), MI->getOperand(3).getImm());
671 return Location;
672}
673
674void X86AsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
675 raw_ostream &O) {
676 // Only the target-dependent form of DBG_VALUE should get here.
677 // Referencing the offset and metadata as NOps-2 and NOps-1 is
678 // probably portable to other targets; frame pointer location is not.
679 unsigned NOps = MI->getNumOperands();
680 assert(NOps==7);
681 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
682 // cast away const; DIetc do not take const operands for some reason.
683 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
684 if (V.getContext().isSubprogram())
685 O << DISubprogram(V.getContext()).getDisplayName() << ":";
686 O << V.getName();
687 O << " <- ";
688 // Frame address. Currently handles register +- offset only.
689 O << '[';
690 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
691 printOperand(MI, 0, O);
692 else
693 O << "undef";
694 O << '+'; printOperand(MI, 3, O);
695 O << ']';
696 O << "+";
697 printOperand(MI, NOps-2, O);
698}
699
700
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000701
702//===----------------------------------------------------------------------===//
703// Target Registry Stuff
704//===----------------------------------------------------------------------===//
705
706static MCInstPrinter *createX86MCInstPrinter(const Target &T,
707 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +0000708 const MCAsmInfo &MAI) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000709 if (SyntaxVariant == 0)
Chris Lattnerd3740872010-04-04 05:04:31 +0000710 return new X86ATTInstPrinter(MAI);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000711 if (SyntaxVariant == 1)
Chris Lattnerd3740872010-04-04 05:04:31 +0000712 return new X86IntelInstPrinter(MAI);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000713 return 0;
714}
715
716// Force static initialization.
717extern "C" void LLVMInitializeX86AsmPrinter() {
718 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
719 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
720
721 TargetRegistry::RegisterMCInstPrinter(TheX86_32Target,createX86MCInstPrinter);
722 TargetRegistry::RegisterMCInstPrinter(TheX86_64Target,createX86MCInstPrinter);
723}