blob: 4e5b7b8804df97ba94379c5be41a6801dedd6d16 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "InstPrinter/X86ATTInstPrinter.h"
Stephen Hines36b56882014-04-23 16:57:46 -070017#include "MCTargetDesc/X86BaseInfo.h"
Stephen Hines36b56882014-04-23 16:57:46 -070018#include "X86InstrInfo.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000019#include "X86MachineFunctionInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/SmallString.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080021#include "llvm/CodeGen/MachineConstantPool.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Stephen Hines36b56882014-04-23 16:57:46 -070023#include "llvm/CodeGen/MachineValueType.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/DerivedTypes.h"
Stephen Hines36b56882014-04-23 16:57:46 -070027#include "llvm/IR/Mangler.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/Module.h"
29#include "llvm/IR/Type.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000030#include "llvm/MC/MCAsmInfo.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000031#include "llvm/MC/MCContext.h"
Chris Lattnerbeb42692010-02-03 06:42:38 +000032#include "llvm/MC/MCExpr.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080033#include "llvm/MC/MCSectionCOFF.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000034#include "llvm/MC/MCSectionMachO.h"
35#include "llvm/MC/MCStreamer.h"
36#include "llvm/MC/MCSymbol.h"
Chris Lattner9fe2c672010-07-14 18:14:33 +000037#include "llvm/Support/COFF.h"
Devang Patel5c1d9412010-08-04 18:06:05 +000038#include "llvm/Support/Debug.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000039#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000040#include "llvm/Support/TargetRegistry.h"
Chris Lattner0dc32ea2009-09-20 07:41:30 +000041using namespace llvm;
42
Chris Lattner0dc32ea2009-09-20 07:41:30 +000043//===----------------------------------------------------------------------===//
44// Primitive Helper Functions.
45//===----------------------------------------------------------------------===//
46
Chris Lattner14c38ec2010-01-28 01:02:27 +000047/// runOnMachineFunction - Emit the function body.
Chris Lattner0dc32ea2009-09-20 07:41:30 +000048///
49bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Stephen Hines37ed9c12014-12-01 14:51:49 -080050 SMShadowTracker.startFunction(MF);
51
Chris Lattner0dc32ea2009-09-20 07:41:30 +000052 SetupMachineFunction(MF);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000053
Stephen Hines36b56882014-04-23 16:57:46 -070054 if (Subtarget->isTargetCOFF()) {
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000055 bool Intrn = MF.getFunction()->hasInternalLinkage();
56 OutStreamer.BeginCOFFSymbolDef(CurrentFnSym);
Jim Grosbach22854b72010-09-15 01:01:45 +000057 OutStreamer.EmitCOFFSymbolStorageClass(Intrn ? COFF::IMAGE_SYM_CLASS_STATIC
Chris Lattner9fe2c672010-07-14 18:14:33 +000058 : COFF::IMAGE_SYM_CLASS_EXTERNAL);
59 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
60 << COFF::SCT_COMPLEX_TYPE_SHIFT);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000061 OutStreamer.EndCOFFSymbolDef();
Chris Lattnerb11caed2010-01-26 23:18:44 +000062 }
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000063
Chris Lattnerb11caed2010-01-26 23:18:44 +000064 // Have common code print out the function header with linkage info etc.
65 EmitFunctionHeader();
Anton Korobeynikov4dd162f2010-02-12 15:28:40 +000066
Chris Lattner14c38ec2010-01-28 01:02:27 +000067 // Emit the rest of the function body.
68 EmitFunctionBody();
Chris Lattner0dc32ea2009-09-20 07:41:30 +000069
70 // We didn't modify anything.
71 return false;
72}
73
74/// printSymbolOperand - Print a raw symbol reference operand. This handles
75/// jump tables, constant pools, global address and external symbols, all of
76/// which print to a label with various suffixes for relocation types etc.
Stephen Hines36b56882014-04-23 16:57:46 -070077static void printSymbolOperand(X86AsmPrinter &P, const MachineOperand &MO,
78 raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +000079 switch (MO.getType()) {
80 default: llvm_unreachable("unknown symbol type!");
Chris Lattner0dc32ea2009-09-20 07:41:30 +000081 case MachineOperand::MO_ConstantPoolIndex:
Stephen Hines36b56882014-04-23 16:57:46 -070082 O << *P.GetCPISymbol(MO.getIndex());
83 P.printOffset(MO.getOffset(), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +000084 break;
85 case MachineOperand::MO_GlobalAddress: {
86 const GlobalValue *GV = MO.getGlobal();
Jim Grosbach22854b72010-09-15 01:01:45 +000087
Chris Lattner5957c842010-01-18 00:59:24 +000088 MCSymbol *GVSym;
Chris Lattner0dc32ea2009-09-20 07:41:30 +000089 if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB)
Stephen Hines36b56882014-04-23 16:57:46 -070090 GVSym = P.getSymbolWithGlobalValueBase(GV, "$stub");
Chris Lattner0dc32ea2009-09-20 07:41:30 +000091 else if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
92 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE ||
93 MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE)
Stephen Hines36b56882014-04-23 16:57:46 -070094 GVSym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner4fb69f42010-01-16 00:51:39 +000095 else
Stephen Hines36b56882014-04-23 16:57:46 -070096 GVSym = P.getSymbol(GV);
Chris Lattner4fb69f42010-01-16 00:51:39 +000097
Chris Lattner0dc32ea2009-09-20 07:41:30 +000098 // Handle dllimport linkage.
99 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Stephen Hines36b56882014-04-23 16:57:46 -0700100 GVSym =
101 P.OutContext.GetOrCreateSymbol(Twine("__imp_") + GVSym->getName());
Jim Grosbach22854b72010-09-15 01:01:45 +0000102
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000103 if (MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY ||
104 MO.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE) {
Stephen Hines36b56882014-04-23 16:57:46 -0700105 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Jim Grosbach22854b72010-09-15 01:01:45 +0000106 MachineModuleInfoImpl::StubValueTy &StubSym =
Stephen Hines36b56882014-04-23 16:57:46 -0700107 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700108 if (!StubSym.getPointer())
Bill Wendlingcebae362010-03-10 22:34:10 +0000109 StubSym = MachineModuleInfoImpl::
Stephen Hines36b56882014-04-23 16:57:46 -0700110 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000111 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE){
Stephen Hines36b56882014-04-23 16:57:46 -0700112 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000113 MachineModuleInfoImpl::StubValueTy &StubSym =
Stephen Hines36b56882014-04-23 16:57:46 -0700114 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(
115 Sym);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700116 if (!StubSym.getPointer())
Bill Wendlingcebae362010-03-10 22:34:10 +0000117 StubSym = MachineModuleInfoImpl::
Stephen Hines36b56882014-04-23 16:57:46 -0700118 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000119 } else if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
Stephen Hines36b56882014-04-23 16:57:46 -0700120 MCSymbol *Sym = P.getSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingcebae362010-03-10 22:34:10 +0000121 MachineModuleInfoImpl::StubValueTy &StubSym =
Stephen Hines36b56882014-04-23 16:57:46 -0700122 P.MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700123 if (!StubSym.getPointer())
Bill Wendlingcebae362010-03-10 22:34:10 +0000124 StubSym = MachineModuleInfoImpl::
Stephen Hines36b56882014-04-23 16:57:46 -0700125 StubValueTy(P.getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000126 }
Jim Grosbach22854b72010-09-15 01:01:45 +0000127
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000128 // If the name begins with a dollar-sign, enclose it in parens. We do this
129 // to avoid having it look like an integer immediate to the assembler.
Chris Lattner4fb69f42010-01-16 00:51:39 +0000130 if (GVSym->getName()[0] != '$')
Chris Lattner10b318b2010-01-17 21:43:43 +0000131 O << *GVSym;
132 else
133 O << '(' << *GVSym << ')';
Stephen Hines36b56882014-04-23 16:57:46 -0700134 P.printOffset(MO.getOffset(), O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000135 break;
136 }
137 }
Jim Grosbach22854b72010-09-15 01:01:45 +0000138
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000139 switch (MO.getTargetFlags()) {
140 default:
141 llvm_unreachable("Unknown target flag on GV operand");
142 case X86II::MO_NO_FLAG: // No flag.
143 break;
144 case X86II::MO_DARWIN_NONLAZY:
145 case X86II::MO_DLLIMPORT:
146 case X86II::MO_DARWIN_STUB:
147 // These affect the name of the symbol, not any suffix.
148 break;
149 case X86II::MO_GOT_ABSOLUTE_ADDRESS:
Stephen Hines36b56882014-04-23 16:57:46 -0700150 O << " + [.-" << *P.MF->getPICBaseSymbol() << ']';
Jim Grosbach22854b72010-09-15 01:01:45 +0000151 break;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000152 case X86II::MO_PIC_BASE_OFFSET:
153 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
154 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
Stephen Hines36b56882014-04-23 16:57:46 -0700155 O << '-' << *P.MF->getPICBaseSymbol();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000156 break;
157 case X86II::MO_TLSGD: O << "@TLSGD"; break;
Hans Wennborgf0234fc2012-06-01 16:27:21 +0000158 case X86II::MO_TLSLD: O << "@TLSLD"; break;
159 case X86II::MO_TLSLDM: O << "@TLSLDM"; break;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000160 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
161 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
162 case X86II::MO_TPOFF: O << "@TPOFF"; break;
Hans Wennborgf0234fc2012-06-01 16:27:21 +0000163 case X86II::MO_DTPOFF: O << "@DTPOFF"; break;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000164 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
Hans Wennborg228756c2012-05-11 10:11:01 +0000165 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000166 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
167 case X86II::MO_GOT: O << "@GOT"; break;
168 case X86II::MO_GOTOFF: O << "@GOTOFF"; break;
169 case X86II::MO_PLT: O << "@PLT"; break;
Eric Christopher30ef0e52010-06-03 04:07:48 +0000170 case X86II::MO_TLVP: O << "@TLVP"; break;
171 case X86II::MO_TLVP_PIC_BASE:
Stephen Hines36b56882014-04-23 16:57:46 -0700172 O << "@TLVP" << '-' << *P.MF->getPICBaseSymbol();
Eric Christopher30ef0e52010-06-03 04:07:48 +0000173 break;
Benjamin Kramer42734cf2013-03-30 16:21:50 +0000174 case X86II::MO_SECREL: O << "@SECREL32"; break;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000175 }
176}
177
Stephen Hines36b56882014-04-23 16:57:46 -0700178static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
179 unsigned OpNo, raw_ostream &O,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700180 const char *Modifier = nullptr, unsigned AsmVariant = 0);
Stephen Hines36b56882014-04-23 16:57:46 -0700181
Chad Rosier35c68892012-09-10 22:50:57 +0000182/// printPCRelImm - This is used to print an immediate value that ends up
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000183/// being encoded as a pc-relative value. These print slightly differently, for
184/// example, a $ is not emitted.
Stephen Hines36b56882014-04-23 16:57:46 -0700185static void printPCRelImm(X86AsmPrinter &P, const MachineInstr *MI,
186 unsigned OpNo, raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000187 const MachineOperand &MO = MI->getOperand(OpNo);
188 switch (MO.getType()) {
189 default: llvm_unreachable("Unknown pcrel immediate operand");
Dale Johannesen323200d2010-07-16 18:35:46 +0000190 case MachineOperand::MO_Register:
191 // pc-relativeness was handled when computing the value in the reg.
Stephen Hines36b56882014-04-23 16:57:46 -0700192 printOperand(P, MI, OpNo, O);
Dale Johannesen323200d2010-07-16 18:35:46 +0000193 return;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000194 case MachineOperand::MO_Immediate:
195 O << MO.getImm();
196 return;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000197 case MachineOperand::MO_GlobalAddress:
Stephen Hines36b56882014-04-23 16:57:46 -0700198 printSymbolOperand(P, MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000199 return;
200 }
201}
202
Stephen Hines36b56882014-04-23 16:57:46 -0700203static void printOperand(X86AsmPrinter &P, const MachineInstr *MI,
204 unsigned OpNo, raw_ostream &O, const char *Modifier,
205 unsigned AsmVariant) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000206 const MachineOperand &MO = MI->getOperand(OpNo);
207 switch (MO.getType()) {
208 default: llvm_unreachable("unknown operand type!");
209 case MachineOperand::MO_Register: {
Chad Rosier3b132fa2012-09-10 21:10:49 +0000210 // FIXME: Enumerating AsmVariant, so we can remove magic number.
211 if (AsmVariant == 0) O << '%';
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000212 unsigned Reg = MO.getReg();
213 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Craig Topperf4d25a22012-09-30 19:49:56 +0000214 MVT::SimpleValueType VT = (strcmp(Modifier+6,"64") == 0) ?
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000215 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
216 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
217 Reg = getX86SubSuperRegister(Reg, VT);
218 }
219 O << X86ATTInstPrinter::getRegisterName(Reg);
220 return;
221 }
222
223 case MachineOperand::MO_Immediate:
Chad Rosier7b0bc3f2013-02-13 21:33:44 +0000224 if (AsmVariant == 0) O << '$';
225 O << MO.getImm();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000226 return;
227
Stephen Hines36b56882014-04-23 16:57:46 -0700228 case MachineOperand::MO_GlobalAddress: {
Chad Rosier7b0bc3f2013-02-13 21:33:44 +0000229 if (AsmVariant == 0) O << '$';
Stephen Hines36b56882014-04-23 16:57:46 -0700230 printSymbolOperand(P, MO, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000231 break;
232 }
233 }
234}
235
Stephen Hines36b56882014-04-23 16:57:46 -0700236static void printLeaMemReference(X86AsmPrinter &P, const MachineInstr *MI,
237 unsigned Op, raw_ostream &O,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700238 const char *Modifier = nullptr) {
Stephen Hines36b56882014-04-23 16:57:46 -0700239 const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
240 const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
241 const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000242
243 // If we really don't want to print out (rip), don't.
244 bool HasBaseReg = BaseReg.getReg() != 0;
245 if (HasBaseReg && Modifier && !strcmp(Modifier, "no-rip") &&
246 BaseReg.getReg() == X86::RIP)
247 HasBaseReg = false;
Jim Grosbach22854b72010-09-15 01:01:45 +0000248
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000249 // HasParenPart - True if we will print out the () part of the mem ref.
250 bool HasParenPart = IndexReg.getReg() || HasBaseReg;
Jim Grosbach22854b72010-09-15 01:01:45 +0000251
Stephen Hines36b56882014-04-23 16:57:46 -0700252 switch (DispSpec.getType()) {
253 default:
254 llvm_unreachable("unknown operand type!");
255 case MachineOperand::MO_Immediate: {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000256 int DispVal = DispSpec.getImm();
257 if (DispVal || !HasParenPart)
258 O << DispVal;
Stephen Hines36b56882014-04-23 16:57:46 -0700259 break;
260 }
261 case MachineOperand::MO_GlobalAddress:
262 case MachineOperand::MO_ConstantPoolIndex:
263 printSymbolOperand(P, DispSpec, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000264 }
265
Rafael Espindola03277e72011-01-01 20:58:46 +0000266 if (Modifier && strcmp(Modifier, "H") == 0)
267 O << "+8";
268
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000269 if (HasParenPart) {
270 assert(IndexReg.getReg() != X86::ESP &&
271 "X86 doesn't allow scaling by ESP");
272
273 O << '(';
274 if (HasBaseReg)
Stephen Hines36b56882014-04-23 16:57:46 -0700275 printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000276
277 if (IndexReg.getReg()) {
278 O << ',';
Stephen Hines36b56882014-04-23 16:57:46 -0700279 printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier);
280 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000281 if (ScaleVal != 1)
282 O << ',' << ScaleVal;
283 }
284 O << ')';
285 }
286}
287
Stephen Hines36b56882014-04-23 16:57:46 -0700288static void printMemReference(X86AsmPrinter &P, const MachineInstr *MI,
289 unsigned Op, raw_ostream &O,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700290 const char *Modifier = nullptr) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000291 assert(isMem(MI, Op) && "Invalid memory reference!");
Stephen Hines36b56882014-04-23 16:57:46 -0700292 const MachineOperand &Segment = MI->getOperand(Op+X86::AddrSegmentReg);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000293 if (Segment.getReg()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700294 printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000295 O << ':';
296 }
Stephen Hines36b56882014-04-23 16:57:46 -0700297 printLeaMemReference(P, MI, Op, O, Modifier);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000298}
299
Stephen Hines36b56882014-04-23 16:57:46 -0700300static void printIntelMemReference(X86AsmPrinter &P, const MachineInstr *MI,
301 unsigned Op, raw_ostream &O,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700302 const char *Modifier = nullptr,
Stephen Hines36b56882014-04-23 16:57:46 -0700303 unsigned AsmVariant = 1) {
304 const MachineOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
305 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
306 const MachineOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
307 const MachineOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
308 const MachineOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg);
Andrew Trickcf940ce2013-10-31 17:18:07 +0000309
Chad Rosier34448ae2012-10-03 22:06:44 +0000310 // If this has a segment register, print it.
311 if (SegReg.getReg()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700312 printOperand(P, MI, Op+X86::AddrSegmentReg, O, Modifier, AsmVariant);
Chad Rosier34448ae2012-10-03 22:06:44 +0000313 O << ':';
314 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000315
Chad Rosier34448ae2012-10-03 22:06:44 +0000316 O << '[';
Andrew Trickcf940ce2013-10-31 17:18:07 +0000317
Chad Rosier34448ae2012-10-03 22:06:44 +0000318 bool NeedPlus = false;
319 if (BaseReg.getReg()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700320 printOperand(P, MI, Op+X86::AddrBaseReg, O, Modifier, AsmVariant);
Chad Rosier34448ae2012-10-03 22:06:44 +0000321 NeedPlus = true;
322 }
Andrew Trickcf940ce2013-10-31 17:18:07 +0000323
Chad Rosier34448ae2012-10-03 22:06:44 +0000324 if (IndexReg.getReg()) {
325 if (NeedPlus) O << " + ";
326 if (ScaleVal != 1)
327 O << ScaleVal << '*';
Stephen Hines36b56882014-04-23 16:57:46 -0700328 printOperand(P, MI, Op+X86::AddrIndexReg, O, Modifier, AsmVariant);
Chad Rosier34448ae2012-10-03 22:06:44 +0000329 NeedPlus = true;
330 }
331
Chad Rosier7b0bc3f2013-02-13 21:33:44 +0000332 if (!DispSpec.isImm()) {
333 if (NeedPlus) O << " + ";
Stephen Hines36b56882014-04-23 16:57:46 -0700334 printOperand(P, MI, Op+X86::AddrDisp, O, Modifier, AsmVariant);
Chad Rosier7b0bc3f2013-02-13 21:33:44 +0000335 } else {
336 int64_t DispVal = DispSpec.getImm();
337 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
338 if (NeedPlus) {
339 if (DispVal > 0)
340 O << " + ";
341 else {
342 O << " - ";
343 DispVal = -DispVal;
344 }
Chad Rosier34448ae2012-10-03 22:06:44 +0000345 }
Chad Rosier7b0bc3f2013-02-13 21:33:44 +0000346 O << DispVal;
Chad Rosier34448ae2012-10-03 22:06:44 +0000347 }
Chad Rosier7b0bc3f2013-02-13 21:33:44 +0000348 }
Chad Rosier34448ae2012-10-03 22:06:44 +0000349 O << ']';
350}
351
Stephen Hines36b56882014-04-23 16:57:46 -0700352static bool printAsmMRegister(X86AsmPrinter &P, const MachineOperand &MO,
353 char Mode, 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;
Stephen Hines36b56882014-04-23 16:57:46 -0700369 case 'q':
370 // Print 64-bit register names if 64-bit integer registers are available.
371 // Otherwise, print 32-bit register names.
372 MVT::SimpleValueType Ty = P.getSubtarget().is64Bit() ? MVT::i64 : MVT::i32;
373 Reg = getX86SubSuperRegister(Reg, Ty);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000374 break;
375 }
376
377 O << '%' << X86ATTInstPrinter::getRegisterName(Reg);
378 return false;
379}
380
381/// PrintAsmOperand - Print out an operand for an inline asm expression.
382///
383bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
384 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000385 const char *ExtraCode, raw_ostream &O) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000386 // Does this asm operand have a single letter operand modifier?
387 if (ExtraCode && ExtraCode[0]) {
388 if (ExtraCode[1] != 0) return true; // Unknown modifier.
389
390 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Grosbach22854b72010-09-15 01:01:45 +0000391
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000392 switch (ExtraCode[0]) {
Jack Carter0518fca2012-06-26 13:49:27 +0000393 default:
394 // See if this is a generic print operand
395 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000396 case 'a': // This is an address. Currently only 'i' and 'r' are expected.
Stephen Hines36b56882014-04-23 16:57:46 -0700397 switch (MO.getType()) {
398 default:
399 return true;
400 case MachineOperand::MO_Immediate:
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000401 O << MO.getImm();
402 return false;
Stephen Hines36b56882014-04-23 16:57:46 -0700403 case MachineOperand::MO_ConstantPoolIndex:
404 case MachineOperand::MO_JumpTableIndex:
405 case MachineOperand::MO_ExternalSymbol:
406 llvm_unreachable("unexpected operand type!");
407 case MachineOperand::MO_GlobalAddress:
408 printSymbolOperand(*this, MO, O);
Dale Johannesene2b448c2010-07-06 23:27:00 +0000409 if (Subtarget->isPICStyleRIPRel())
410 O << "(%rip)";
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000411 return false;
Stephen Hines36b56882014-04-23 16:57:46 -0700412 case MachineOperand::MO_Register:
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000413 O << '(';
Stephen Hines36b56882014-04-23 16:57:46 -0700414 printOperand(*this, MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000415 O << ')';
416 return false;
417 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000418
419 case 'c': // Don't print "$" before a global var name or constant.
Stephen Hines36b56882014-04-23 16:57:46 -0700420 switch (MO.getType()) {
421 default:
422 printOperand(*this, MI, OpNo, O);
423 break;
424 case MachineOperand::MO_Immediate:
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000425 O << MO.getImm();
Stephen Hines36b56882014-04-23 16:57:46 -0700426 break;
427 case MachineOperand::MO_ConstantPoolIndex:
428 case MachineOperand::MO_JumpTableIndex:
429 case MachineOperand::MO_ExternalSymbol:
430 llvm_unreachable("unexpected operand type!");
431 case MachineOperand::MO_GlobalAddress:
432 printSymbolOperand(*this, MO, O);
433 break;
434 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000435 return false;
436
437 case 'A': // Print '*' before a register (it must be a register)
438 if (MO.isReg()) {
439 O << '*';
Stephen Hines36b56882014-04-23 16:57:46 -0700440 printOperand(*this, MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000441 return false;
442 }
443 return true;
444
445 case 'b': // Print QImode register
446 case 'h': // Print QImode high register
447 case 'w': // Print HImode register
448 case 'k': // Print SImode register
449 case 'q': // Print DImode register
450 if (MO.isReg())
Stephen Hines36b56882014-04-23 16:57:46 -0700451 return printAsmMRegister(*this, MO, ExtraCode[0], O);
452 printOperand(*this, MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000453 return false;
454
455 case 'P': // This is the operand of a call, treat specially.
Stephen Hines36b56882014-04-23 16:57:46 -0700456 printPCRelImm(*this, MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000457 return false;
458
459 case 'n': // Negate the immediate or print a '-' before the operand.
460 // Note: this is a temporary solution. It should be handled target
461 // independently as part of the 'MC' work.
462 if (MO.isImm()) {
463 O << -MO.getImm();
464 return false;
465 }
466 O << '-';
467 }
468 }
469
Stephen Hinesdce4a402014-05-29 02:49:00 -0700470 printOperand(*this, MI, OpNo, O, /*Modifier*/ nullptr, AsmVariant);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000471 return false;
472}
473
474bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
475 unsigned OpNo, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000476 const char *ExtraCode,
477 raw_ostream &O) {
Chad Rosier34448ae2012-10-03 22:06:44 +0000478 if (AsmVariant) {
Stephen Hines36b56882014-04-23 16:57:46 -0700479 printIntelMemReference(*this, MI, OpNo, O);
Chad Rosier34448ae2012-10-03 22:06:44 +0000480 return false;
481 }
482
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000483 if (ExtraCode && ExtraCode[0]) {
484 if (ExtraCode[1] != 0) return true; // Unknown modifier.
485
486 switch (ExtraCode[0]) {
487 default: return true; // Unknown modifier.
488 case 'b': // Print QImode register
489 case 'h': // Print QImode high register
490 case 'w': // Print HImode register
491 case 'k': // Print SImode register
492 case 'q': // Print SImode register
493 // These only apply to registers, ignore on mem.
494 break;
Rafael Espindola03277e72011-01-01 20:58:46 +0000495 case 'H':
Stephen Hines36b56882014-04-23 16:57:46 -0700496 printMemReference(*this, MI, OpNo, O, "H");
Rafael Espindola03277e72011-01-01 20:58:46 +0000497 return false;
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000498 case 'P': // Don't print @PLT, but do print as memory.
Stephen Hines36b56882014-04-23 16:57:46 -0700499 printMemReference(*this, MI, OpNo, O, "no-rip");
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000500 return false;
501 }
502 }
Stephen Hines36b56882014-04-23 16:57:46 -0700503 printMemReference(*this, MI, OpNo, O);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000504 return false;
505}
506
Chris Lattner1bd1e6d2010-03-13 02:10:00 +0000507void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
Stephen Hines36b56882014-04-23 16:57:46 -0700508 if (Subtarget->isTargetMacho())
Chris Lattner1bd1e6d2010-03-13 02:10:00 +0000509 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Reid Kleckner28860822013-09-17 23:18:05 +0000510
Hans Wennborgab887bf2013-10-18 02:14:40 +0000511 if (Subtarget->isTargetCOFF()) {
Reid Kleckner28860822013-09-17 23:18:05 +0000512 // Emit an absolute @feat.00 symbol. This appears to be some kind of
513 // compiler features bitfield read by link.exe.
514 if (!Subtarget->is64Bit()) {
515 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(StringRef("@feat.00"));
516 OutStreamer.BeginCOFFSymbolDef(S);
517 OutStreamer.EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
518 OutStreamer.EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_NULL);
519 OutStreamer.EndCOFFSymbolDef();
520 // According to the PE-COFF spec, the LSB of this value marks the object
521 // for "registered SEH". This means that all SEH handler entry points
522 // must be registered in .sxdata. Use of any unregistered handlers will
523 // cause the process to terminate immediately. LLVM does not know how to
524 // register any SEH handlers, so its object files should be safe.
525 S->setAbsolute();
Reid Klecknera7b7a7d2013-11-18 23:08:12 +0000526 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
Reid Kleckner28860822013-09-17 23:18:05 +0000527 OutStreamer.EmitAssignment(
528 S, MCConstantExpr::Create(int64_t(1), MMI->getContext()));
529 }
530 }
Chris Lattner1bd1e6d2010-03-13 02:10:00 +0000531}
532
Stephen Hinesdce4a402014-05-29 02:49:00 -0700533static void
534emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
535 MachineModuleInfoImpl::StubValueTy &MCSym) {
536 // L_foo$stub:
537 OutStreamer.EmitLabel(StubLabel);
538 // .indirect_symbol _foo
539 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
540
541 if (MCSym.getInt())
542 // External to current translation unit.
543 OutStreamer.EmitIntValue(0, 4/*size*/);
544 else
545 // Internal to current translation unit.
546 //
547 // When we place the LSDA into the TEXT section, the type info
548 // pointers need to be indirect and pc-rel. We accomplish this by
549 // using NLPs; however, sometimes the types are local to the file.
550 // We need to fill in the value for the NLP in those cases.
551 OutStreamer.EmitValue(
552 MCSymbolRefExpr::Create(MCSym.getPointer(), OutStreamer.getContext()),
553 4 /*size*/);
554}
555
Stephen Hines37ed9c12014-12-01 14:51:49 -0800556MCSymbol *X86AsmPrinter::GetCPISymbol(unsigned CPID) const {
557 if (Subtarget->isTargetKnownWindowsMSVC()) {
558 const MachineConstantPoolEntry &CPE =
559 MF->getConstantPool()->getConstants()[CPID];
560 if (!CPE.isMachineConstantPoolEntry()) {
561 SectionKind Kind =
562 CPE.getSectionKind(TM.getSubtargetImpl()->getDataLayout());
563 const Constant *C = CPE.Val.ConstVal;
564 if (const MCSectionCOFF *S = dyn_cast<MCSectionCOFF>(
565 getObjFileLowering().getSectionForConstant(Kind, C))) {
566 if (MCSymbol *Sym = S->getCOMDATSymbol()) {
567 if (Sym->isUndefined())
568 OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
569 return Sym;
570 }
571 }
572 }
573 }
574
575 return AsmPrinter::GetCPISymbol(CPID);
576}
577
Stephen Hinesdce4a402014-05-29 02:49:00 -0700578void X86AsmPrinter::GenerateExportDirective(const MCSymbol *Sym, bool IsData) {
579 SmallString<128> Directive;
580 raw_svector_ostream OS(Directive);
581 StringRef Name = Sym->getName();
582
583 if (Subtarget->isTargetKnownWindowsMSVC())
584 OS << " /EXPORT:";
585 else
586 OS << " -export:";
587
588 if ((Subtarget->isTargetWindowsGNU() || Subtarget->isTargetWindowsCygwin()) &&
589 (Name[0] == getDataLayout().getGlobalPrefix()))
590 Name = Name.drop_front();
591
592 OS << Name;
593
594 if (IsData) {
595 if (Subtarget->isTargetKnownWindowsMSVC())
596 OS << ",DATA";
597 else
598 OS << ",data";
599 }
600
601 OS.flush();
602 OutStreamer.EmitBytes(Directive);
603}
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000604
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000605void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
Stephen Hines36b56882014-04-23 16:57:46 -0700606 if (Subtarget->isTargetMacho()) {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000607 // All darwin targets use mach-o.
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000608 MachineModuleInfoMachO &MMIMacho =
609 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbach22854b72010-09-15 01:01:45 +0000610
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000611 // Output stubs for dynamically-linked functions.
612 MachineModuleInfoMachO::SymbolListTy Stubs;
613
614 Stubs = MMIMacho.GetFnStubList();
615 if (!Stubs.empty()) {
Jim Grosbach22854b72010-09-15 01:01:45 +0000616 const MCSection *TheSection =
Chris Lattner22772212010-04-08 20:40:11 +0000617 OutContext.getMachOSection("__IMPORT", "__jump_table",
Stephen Hines36b56882014-04-23 16:57:46 -0700618 MachO::S_SYMBOL_STUBS |
619 MachO::S_ATTR_SELF_MODIFYING_CODE |
620 MachO::S_ATTR_PURE_INSTRUCTIONS,
Chris Lattner22772212010-04-08 20:40:11 +0000621 5, SectionKind::getMetadata());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000622 OutStreamer.SwitchSection(TheSection);
623
Stephen Hinesdce4a402014-05-29 02:49:00 -0700624 for (const auto &Stub : Stubs) {
Chris Lattnerbeb42692010-02-03 06:42:38 +0000625 // L_foo$stub:
Stephen Hinesdce4a402014-05-29 02:49:00 -0700626 OutStreamer.EmitLabel(Stub.first);
Chris Lattnerbeb42692010-02-03 06:42:38 +0000627 // .indirect_symbol _foo
Stephen Hinesdce4a402014-05-29 02:49:00 -0700628 OutStreamer.EmitSymbolAttribute(Stub.second.getPointer(),
Bill Wendlingcebae362010-03-10 22:34:10 +0000629 MCSA_IndirectSymbol);
Jeffrey Yasskincda2a142011-08-30 20:53:29 +0000630 // hlt; hlt; hlt; hlt; hlt hlt = 0xf4.
631 const char HltInsts[] = "\xf4\xf4\xf4\xf4\xf4";
Eric Christopher68ca5622013-01-09 01:57:54 +0000632 OutStreamer.EmitBytes(StringRef(HltInsts, 5));
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000633 }
Jim Grosbach22854b72010-09-15 01:01:45 +0000634
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000635 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000636 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000637 }
638
639 // Output stubs for external and common global variables.
640 Stubs = MMIMacho.GetGVStubList();
641 if (!Stubs.empty()) {
Jim Grosbach22854b72010-09-15 01:01:45 +0000642 const MCSection *TheSection =
Chris Lattner22772212010-04-08 20:40:11 +0000643 OutContext.getMachOSection("__IMPORT", "__pointers",
Stephen Hines36b56882014-04-23 16:57:46 -0700644 MachO::S_NON_LAZY_SYMBOL_POINTERS,
Chris Lattner22772212010-04-08 20:40:11 +0000645 SectionKind::getMetadata());
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000646 OutStreamer.SwitchSection(TheSection);
647
Stephen Hinesdce4a402014-05-29 02:49:00 -0700648 for (auto &Stub : Stubs)
649 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
650
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000651 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000652 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000653 }
654
655 Stubs = MMIMacho.GetHiddenGVStubList();
656 if (!Stubs.empty()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700657 const MCSection *TheSection =
658 OutContext.getMachOSection("__IMPORT", "__pointers",
659 MachO::S_NON_LAZY_SYMBOL_POINTERS,
660 SectionKind::getMetadata());
661 OutStreamer.SwitchSection(TheSection);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000662
Stephen Hinesdce4a402014-05-29 02:49:00 -0700663 for (auto &Stub : Stubs)
664 emitNonLazySymbolPointer(OutStreamer, Stub.first, Stub.second);
665
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000666 Stubs.clear();
Chris Lattnerbeb42692010-02-03 06:42:38 +0000667 OutStreamer.AddBlankLine();
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000668 }
669
Andrew Trick3d74dea2013-10-31 22:11:56 +0000670 SM.serializeToStackMapSection();
671
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000672 // Funny Darwin hack: This flag tells the linker that no global symbols
673 // contain code that falls through to other global symbols (e.g. the obvious
674 // implementation of multiple entry points). If this doesn't occur, the
675 // linker can safely perform dead code stripping. Since LLVM never
676 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000677 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000678 }
679
Stephen Hines36b56882014-04-23 16:57:46 -0700680 if (Subtarget->isTargetKnownWindowsMSVC() && MMI->usesVAFloatArgument()) {
Michael J. Spencerc5274072010-10-23 09:06:59 +0000681 StringRef SymbolName = Subtarget->is64Bit() ? "_fltused" : "__fltused";
682 MCSymbol *S = MMI->getContext().GetOrCreateSymbol(SymbolName);
Michael J. Spencer84ac4d52010-10-16 08:25:41 +0000683 OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
684 }
685
Stephen Hines36b56882014-04-23 16:57:46 -0700686 if (Subtarget->isTargetCOFF()) {
Nathan Jeffords071de922010-05-09 05:52:28 +0000687 // Necessary for dllexport support
688 std::vector<const MCSymbol*> DLLExportedFns, DLLExportedGlobals;
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000689
Stephen Hinesdce4a402014-05-29 02:49:00 -0700690 for (const auto &Function : M)
691 if (Function.hasDLLExportStorageClass())
692 DLLExportedFns.push_back(getSymbol(&Function));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000693
Stephen Hinesdce4a402014-05-29 02:49:00 -0700694 for (const auto &Global : M.globals())
695 if (Global.hasDLLExportStorageClass())
696 DLLExportedGlobals.push_back(getSymbol(&Global));
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000697
Stephen Hinesdce4a402014-05-29 02:49:00 -0700698 for (const auto &Alias : M.aliases()) {
699 if (!Alias.hasDLLExportStorageClass())
Stephen Hines36b56882014-04-23 16:57:46 -0700700 continue;
701
Stephen Hinesdce4a402014-05-29 02:49:00 -0700702 if (Alias.getType()->getElementType()->isFunctionTy())
703 DLLExportedFns.push_back(getSymbol(&Alias));
704 else
705 DLLExportedGlobals.push_back(getSymbol(&Alias));
Stephen Hines36b56882014-04-23 16:57:46 -0700706 }
707
Nathan Jeffords071de922010-05-09 05:52:28 +0000708 // Output linker support code for dllexported globals on windows.
709 if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700710 const TargetLoweringObjectFileCOFF &TLOFCOFF =
711 static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
712
Nathan Jeffords071de922010-05-09 05:52:28 +0000713 OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
Anton Korobeynikov4fac9472009-10-15 22:36:18 +0000714
Stephen Hinesdce4a402014-05-29 02:49:00 -0700715 for (auto & Symbol : DLLExportedGlobals)
716 GenerateExportDirective(Symbol, /*IsData=*/true);
717 for (auto & Symbol : DLLExportedFns)
718 GenerateExportDirective(Symbol, /*IsData=*/false);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000719 }
720 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000721
722 if (Subtarget->isTargetELF()) {
Dan Gohman0d805c32010-04-17 16:44:48 +0000723 const TargetLoweringObjectFileELF &TLOFELF =
724 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000725
726 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
727
728 // Output stubs for external and common global variables.
729 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
730 if (!Stubs.empty()) {
731 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800732 const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000733
Stephen Hinesdce4a402014-05-29 02:49:00 -0700734 for (const auto &Stub : Stubs) {
735 OutStreamer.EmitLabel(Stub.first);
736 OutStreamer.EmitSymbolValue(Stub.second.getPointer(),
Eric Christopher68ca5622013-01-09 01:57:54 +0000737 TD->getPointerSize());
Chris Lattnerc82d9c42010-04-04 05:35:04 +0000738 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000739 Stubs.clear();
740 }
Stephen Hines37ed9c12014-12-01 14:51:49 -0800741
742 SM.serializeToStackMapSection();
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000743 }
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000744}
745
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000746//===----------------------------------------------------------------------===//
747// Target Registry Stuff
748//===----------------------------------------------------------------------===//
749
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000750// Force static initialization.
Jim Grosbach22854b72010-09-15 01:01:45 +0000751extern "C" void LLVMInitializeX86AsmPrinter() {
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000752 RegisterAsmPrinter<X86AsmPrinter> X(TheX86_32Target);
753 RegisterAsmPrinter<X86AsmPrinter> Y(TheX86_64Target);
Chris Lattner0dc32ea2009-09-20 07:41:30 +0000754}