blob: 07670ea62beaaba321bf1276bbdc4491a42f65bf [file] [log] [blame]
Dan Gohman8bc49c22007-06-25 15:11:25 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to AT&T assembly -----===//
Chris Lattnerb36cbd02005-07-01 22:44:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86ATTAsmPrinter.h"
18#include "X86.h"
Anton Korobeynikovd05ca652007-01-16 16:41:57 +000019#include "X86COFF.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000020#include "X86MachineFunctionInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000021#include "X86TargetMachine.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000022#include "X86TargetAsmInfo.h"
Anton Korobeynikov7f705592007-01-12 19:20:47 +000023#include "llvm/ADT/StringExtras.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000024#include "llvm/CallingConv.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000025#include "llvm/Module.h"
26#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000027#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattner95b2c7d2006-12-19 22:59:26 +000029#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000031
Chris Lattner95b2c7d2006-12-19 22:59:26 +000032STATISTIC(EmittedInsts, "Number of machine instrs printed");
33
Evan Cheng071b9d52007-01-18 01:49:58 +000034static std::string computePICLabel(unsigned FnNum,
35 const TargetAsmInfo *TAI,
36 const X86Subtarget* Subtarget) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +000037 std::string label;
Evan Cheng071b9d52007-01-18 01:49:58 +000038 if (Subtarget->isTargetDarwin())
39 label = "\"L" + utostr_32(FnNum) + "$pb\"";
40 else if (Subtarget->isTargetELF())
41 label = ".Lllvm$" + utostr_32(FnNum) + "$piclabel";
42 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +000043 assert(0 && "Don't know how to print PIC label!\n");
44
45 return label;
46}
47
Chris Lattnerafbfded2006-10-05 02:43:52 +000048/// getSectionForFunction - Return the section that we should emit the
49/// specified function body into.
50std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
51 switch (F.getLinkage()) {
52 default: assert(0 && "Unknown linkage type!");
53 case Function::InternalLinkage:
54 case Function::DLLExportLinkage:
55 case Function::ExternalLinkage:
56 return TAI->getTextSection();
57 case Function::WeakLinkage:
58 case Function::LinkOnceLinkage:
59 if (Subtarget->isTargetDarwin()) {
60 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
Anton Korobeynikov317848f2007-01-03 11:43:14 +000061 } else if (Subtarget->isTargetCygMing()) {
Dan Gohmanaf67ea72007-06-14 15:00:27 +000062 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"";
Chris Lattnerafbfded2006-10-05 02:43:52 +000063 } else {
64 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
Dan Gohmanaf67ea72007-06-14 15:00:27 +000065 ",\"ax\",@progbits";
Chris Lattnerafbfded2006-10-05 02:43:52 +000066 }
67 }
68}
69
Chris Lattnerb36cbd02005-07-01 22:44:09 +000070/// runOnMachineFunction - This uses the printMachineInstruction()
71/// method to print assembly for each instruction.
72///
73bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +000074 if (TAI->doesSupportDebugInformation()) {
Jim Laskeye29c2f52006-07-19 11:54:50 +000075 // Let PassManager know we need debug information and relay
Jim Laskey44c3b9f2007-01-26 21:22:28 +000076 // the MachineModuleInfo address on to DwarfWriter.
77 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Jim Laskeye29c2f52006-07-19 11:54:50 +000078 }
Evan Cheng3c992d22006-03-07 02:02:57 +000079
Chris Lattner8b8b9512005-11-21 07:51:23 +000080 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000081 O << "\n\n";
82
83 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000084 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000085
86 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000087 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000088 unsigned CC = F->getCallingConv();
89
90 // Populate function information map. Actually, We don't want to populate
91 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000092 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +000093 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000094
95 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
96
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +000097 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikovcea9d1d2007-01-06 18:24:26 +000098
Evan Cheng2338c5c2006-02-07 08:38:37 +000099 switch (F->getLinkage()) {
100 default: assert(0 && "Unknown linkage type!");
101 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000102 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
103 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000104 case Function::DLLExportLinkage:
105 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
106 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +0000107 case Function::ExternalLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000108 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000109 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000110 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000111 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000112 case Function::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000113 if (Subtarget->isTargetDarwin()) {
Evan Chengd88ea4d2007-07-25 22:28:16 +0000114 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Evan Chengf1616da2006-02-22 23:59:57 +0000115 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000116 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000117 } else if (Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000118 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000119 O << "\t.globl " << CurrentFnName << "\n";
Anton Korobeynikov66413092007-02-23 01:58:50 +0000120 O << "\t.linkonce discard\n";
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000121 } else {
122 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
123 O << "\t.weak " << CurrentFnName << "\n";
124 }
125 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000126 }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000127 if (F->hasHiddenVisibility()) {
Chris Lattner43bbc5c2007-01-14 06:29:53 +0000128 if (const char *Directive = TAI->getHiddenDirective())
129 O << Directive << CurrentFnName << "\n";
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000130 } else if (F->hasProtectedVisibility()) {
131 if (const char *Directive = TAI->getProtectedDirective())
132 O << Directive << CurrentFnName << "\n";
133 }
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000134
135 if (Subtarget->isTargetELF())
136 O << "\t.type " << CurrentFnName << ",@function\n";
137 else if (Subtarget->isTargetCygMing()) {
138 O << "\t.def\t " << CurrentFnName
139 << ";\t.scl\t" <<
140 (F->getLinkage() == Function::InternalLinkage ? COFF::C_STAT : COFF::C_EXT)
141 << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
142 << ";\t.endef\n";
143 }
144
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000145 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000146 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000147 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000148 (F->getLinkage() == Function::LinkOnceLinkage ||
149 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000150 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000151
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000152 if (TAI->doesSupportDebugInformation()) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000153 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000154 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000155 }
156
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000157 // Print out code for the function.
158 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
159 I != E; ++I) {
160 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000161 if (I->pred_begin() != I->pred_end()) {
162 printBasicBlockLabel(I, true);
163 O << '\n';
164 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000165 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
166 II != E; ++II) {
167 // Print the assembly for the instruction.
168 O << "\t";
169 printMachineInstruction(II);
170 }
171 }
Evan Cheng67afece2006-08-28 22:14:16 +0000172
Jim Laskey563321a2006-09-06 18:34:40 +0000173 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000174 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000175
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000176 if (TAI->doesSupportDebugInformation()) {
Evan Chengd5948812006-03-07 02:23:26 +0000177 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000178 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000179 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000180
Anton Korobeynikov2a07e2f2007-05-05 09:04:50 +0000181 // Print out jump tables referenced by the function.
182 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
183
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000184 // We didn't modify anything.
185 return false;
186}
187
Evan Chengae19abc2007-01-18 22:27:12 +0000188static inline bool printGOT(TargetMachine &TM, const X86Subtarget* ST) {
189 return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
190}
191
192static inline bool printStub(TargetMachine &TM, const X86Subtarget* ST) {
193 return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
194}
195
Chris Lattnera3b8c572006-02-06 23:41:19 +0000196void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000197 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000198 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000199 const MRegisterInfo &RI = *TM.getRegisterInfo();
200 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000201 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000202 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
203 "Virtual registers should not make it this far!");
204 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000205 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000206 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000207 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
208 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
209 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000210 Reg = getX86SubSuperRegister(Reg, VT);
211 }
212 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213 O << (char)tolower(*Name);
214 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000215 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000216
Chris Lattner63b3d712006-05-04 17:21:20 +0000217 case MachineOperand::MO_Immediate:
Chris Lattnerb4828722007-01-25 02:53:24 +0000218 if (!Modifier ||
219 (strcmp(Modifier, "debug") && strcmp(Modifier, "mem")))
Evan Cheng3c992d22006-03-07 02:02:57 +0000220 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000221 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000222 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000223 case MachineOperand::MO_MachineBasicBlock:
224 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000225 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000226 case MachineOperand::MO_JumpTableIndex: {
227 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
228 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000229 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000230 << MO.getJumpTableIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000231
232 if (TM.getRelocationModel() == Reloc::PIC_) {
233 if (Subtarget->isPICStyleStub())
Evan Cheng071b9d52007-01-18 01:49:58 +0000234 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
235 << "$pb\"";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000236 else if (Subtarget->isPICStyleGOT())
237 O << "@GOTOFF";
238 }
239
Evan Chengae19abc2007-01-18 22:27:12 +0000240 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000241 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000242 return;
243 }
Evan Chenga09bd812006-02-26 08:28:12 +0000244 case MachineOperand::MO_ConstantPoolIndex: {
245 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
246 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000247 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000248 << MO.getConstantPoolIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000249
250 if (TM.getRelocationModel() == Reloc::PIC_) {
251 if (Subtarget->isPICStyleStub())
Evan Cheng071b9d52007-01-18 01:49:58 +0000252 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
253 << "$pb\"";
Evan Chengae19abc2007-01-18 22:27:12 +0000254 else if (Subtarget->isPICStyleGOT())
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000255 O << "@GOTOFF";
256 }
257
Evan Chenga09bd812006-02-26 08:28:12 +0000258 int Offset = MO.getOffset();
259 if (Offset > 0)
260 O << "+" << Offset;
261 else if (Offset < 0)
262 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000263
Evan Chengae19abc2007-01-18 22:27:12 +0000264 if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000265 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000266 return;
267 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000268 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000269 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000270 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000271 bool needCloseParen = false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000272
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000273 GlobalValue *GV = MO.getGlobal();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000274 GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
275 bool isThreadLocal = GVar && GVar->isThreadLocal();
276
Evan Cheng25ab6902006-09-08 06:48:29 +0000277 std::string Name = Mang->getValueName(GV);
Chris Lattnere87e1152006-09-26 03:57:53 +0000278 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000279
Dan Gohman2a3250c2007-04-26 21:07:05 +0000280 if (!isMemOp && !isCallOp)
281 O << '$';
282 else if (Name[0] == '$') {
283 // The name begins with a dollar-sign. In order to avoid having it look
284 // like an integer immediate to the assembler, enclose it in parens.
285 O << '(';
286 needCloseParen = true;
287 }
288
Evan Chengae19abc2007-01-18 22:27:12 +0000289 if (printStub(TM, Subtarget)) {
Evan Cheng111354f2007-06-04 18:54:57 +0000290 // Link-once, declaration, or Weakly-linked global variables need
Evan Cheng2338c5c2006-02-07 08:38:37 +0000291 // non-lazily-resolved stubs
Reid Spencer5cbf9852007-01-30 20:08:39 +0000292 if (GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000293 GV->hasWeakLinkage() ||
294 GV->hasLinkOnceLinkage()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000295 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000296 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000297 FnStubs.insert(Name);
Evan Cheng071b9d52007-01-18 01:49:58 +0000298 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000299 } else {
300 GVStubs.insert(Name);
Evan Cheng071b9d52007-01-18 01:49:58 +0000301 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000302 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000303 } else {
Evan Chengae19abc2007-01-18 22:27:12 +0000304 if (GV->hasDLLImportLinkage())
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000305 O << "__imp_";
Evan Cheng25ab6902006-09-08 06:48:29 +0000306 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000307 }
308
Chris Lattner35d86fe2006-07-26 21:12:04 +0000309 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng071b9d52007-01-18 01:49:58 +0000310 O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
311 << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000312 } else {
313 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000314 O << "__imp_";
315 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000317
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000318 if (isCallOp && isa<Function>(GV)) {
Evan Chengae19abc2007-01-18 22:27:12 +0000319 if (printGOT(TM, Subtarget)) {
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000320 // Assemble call via PLT for non-local symbols
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000321 if (!(GV->hasHiddenVisibility() || GV->hasProtectedVisibility()) ||
322 GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000323 O << "@PLT";
324 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000325 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
Anton Korobeynikovd05ca652007-01-16 16:41:57 +0000326 // Save function name for later type emission
327 FnStubs.insert(Name);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000328 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000329 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000330
Evan Cheng6d7d3102006-12-01 07:38:23 +0000331 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000332 ExtWeakSymbols.insert(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000333
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000334 int Offset = MO.getOffset();
335 if (Offset > 0)
336 O << "+" << Offset;
337 else if (Offset < 0)
338 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000339
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000340 if (isThreadLocal) {
341 if (TM.getRelocationModel() == Reloc::PIC_)
342 O << "@TLSGD"; // general dynamic TLS model
343 else
344 if (GV->isDeclaration())
345 O << "@INDNTPOFF"; // initial exec TLS model
346 else
347 O << "@NTPOFF"; // local exec TLS model
348 } else if (isMemOp) {
Evan Chengae19abc2007-01-18 22:27:12 +0000349 if (printGOT(TM, Subtarget)) {
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000350 if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000351 O << "@GOT";
Anton Korobeynikov5032e5a2007-01-17 10:33:08 +0000352 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000353 O << "@GOTOFF";
Evan Chengae19abc2007-01-18 22:27:12 +0000354 } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel) {
Evan Cheng111354f2007-06-04 18:54:57 +0000355 if ((GV->isDeclaration() ||
Evan Chengae19abc2007-01-18 22:27:12 +0000356 GV->hasWeakLinkage() ||
357 GV->hasLinkOnceLinkage()) &&
358 TM.getRelocationModel() != Reloc::Static)
359 O << "@GOTPCREL";
Dan Gohman2a3250c2007-04-26 21:07:05 +0000360
361 if (needCloseParen) {
362 needCloseParen = false;
363 O << ')';
364 }
365
Evan Chengae19abc2007-01-18 22:27:12 +0000366 // Use rip when possible to reduce code size, except when
367 // index or base register are also part of the address. e.g.
368 // foo(%rip)(%rcx,%rax,4) is not legal
369 O << "(%rip)";
370 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 }
372
Dan Gohman2a3250c2007-04-26 21:07:05 +0000373 if (needCloseParen)
374 O << ')';
375
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000376 return;
377 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000378 case MachineOperand::MO_ExternalSymbol: {
379 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman2a3250c2007-04-26 21:07:05 +0000380 bool needCloseParen = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000381 std::string Name(TAI->getGlobalPrefix());
382 Name += MO.getSymbolName();
Evan Chengae19abc2007-01-18 22:27:12 +0000383 if (isCallOp && printStub(TM, Subtarget)) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000384 FnStubs.insert(Name);
Evan Cheng071b9d52007-01-18 01:49:58 +0000385 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
Nate Begeman72b286b2005-07-08 00:23:26 +0000386 return;
387 }
Dan Gohman2a3250c2007-04-26 21:07:05 +0000388 if (!isCallOp)
389 O << '$';
390 else if (Name[0] == '$') {
391 // The name begins with a dollar-sign. In order to avoid having it look
392 // like an integer immediate to the assembler, enclose it in parens.
393 O << '(';
394 needCloseParen = true;
395 }
396
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000397 O << Name;
398
Evan Chengae19abc2007-01-18 22:27:12 +0000399 if (printGOT(TM, Subtarget)) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000400 std::string GOTName(TAI->getGlobalPrefix());
401 GOTName+="_GLOBAL_OFFSET_TABLE_";
402 if (Name == GOTName)
Evan Chengae19abc2007-01-18 22:27:12 +0000403 // HACK! Emit extra offset to PC during printing GOT offset to
404 // compensate for the size of popl instruction. The resulting code
405 // should look like:
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000406 // call .piclabel
407 // piclabel:
408 // popl %some_register
409 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
Evan Cheng071b9d52007-01-18 01:49:58 +0000410 O << " + [.-"
411 << computePICLabel(getFunctionNumber(), TAI, Subtarget) << "]";
Evan Chengae19abc2007-01-18 22:27:12 +0000412
413 if (isCallOp)
414 O << "@PLT";
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000415 }
416
Dan Gohman2a3250c2007-04-26 21:07:05 +0000417 if (needCloseParen)
418 O << ')';
419
Evan Chengae19abc2007-01-18 22:27:12 +0000420 if (!isCallOp && Subtarget->isPICStyleRIPRel())
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 O << "(%rip)";
422
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000423 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000424 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000425 default:
426 O << "<unknown operand type>"; return;
427 }
428}
429
Nate Begeman391c5d22005-11-30 18:54:35 +0000430void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000431 unsigned char value = MI->getOperand(Op).getImmedValue();
432 assert(value <= 7 && "Invalid ssecc argument!");
433 switch (value) {
434 case 0: O << "eq"; break;
435 case 1: O << "lt"; break;
436 case 2: O << "le"; break;
437 case 3: O << "unord"; break;
438 case 4: O << "neq"; break;
439 case 5: O << "nlt"; break;
440 case 6: O << "nle"; break;
441 case 7: O << "ord"; break;
442 }
443}
444
Evan Cheng25ab6902006-09-08 06:48:29 +0000445void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
446 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000447 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner32c9a452007-01-14 00:13:07 +0000448 MachineOperand BaseReg = MI->getOperand(Op);
449 MachineOperand IndexReg = MI->getOperand(Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000450 const MachineOperand &DispSpec = MI->getOperand(Op+3);
451
Evan Cheng28b514392006-12-05 19:50:18 +0000452 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000453 if (DispSpec.isGlobalAddress() ||
454 DispSpec.isConstantPoolIndex() ||
455 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000456 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000457 } else {
458 int DispVal = DispSpec.getImmedValue();
459 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
460 O << DispVal;
461 }
462
463 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner32c9a452007-01-14 00:13:07 +0000464 unsigned ScaleVal = MI->getOperand(Op+1).getImmedValue();
465 unsigned BaseRegOperand = 0, IndexRegOperand = 2;
466
467 // There are cases where we can end up with ESP/RSP in the indexreg slot.
468 // If this happens, swap the base/index register to support assemblers that
469 // don't work when the index is *SP.
470 if (IndexReg.getReg() == X86::ESP || IndexReg.getReg() == X86::RSP) {
471 assert(ScaleVal == 1 && "Scale not supported for stack pointer!");
472 std::swap(BaseReg, IndexReg);
473 std::swap(BaseRegOperand, IndexRegOperand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000474 }
Chris Lattner32c9a452007-01-14 00:13:07 +0000475
476 O << "(";
477 if (BaseReg.getReg())
478 printOperand(MI, Op+BaseRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000479
480 if (IndexReg.getReg()) {
481 O << ",";
Chris Lattner32c9a452007-01-14 00:13:07 +0000482 printOperand(MI, Op+IndexRegOperand, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000483 if (ScaleVal != 1)
484 O << "," << ScaleVal;
485 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000486 O << ")";
487 }
488}
489
Evan Cheng7ccced62006-02-18 00:15:05 +0000490void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng071b9d52007-01-18 01:49:58 +0000491 std::string label = computePICLabel(getFunctionNumber(), TAI, Subtarget);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000492 O << label << "\n" << label << ":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000493}
494
Evan Cheng62f27002006-04-28 23:11:40 +0000495
Evan Cheng55c25f22006-04-28 23:19:39 +0000496bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000497 const char Mode) {
498 const MRegisterInfo &RI = *TM.getRegisterInfo();
499 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000500 switch (Mode) {
501 default: return true; // Unknown mode.
502 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000503 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000504 break;
505 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000506 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000507 break;
508 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000509 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000510 break;
511 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000512 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000513 break;
514 }
515
Evan Cheng8f7f7122006-05-05 05:40:20 +0000516 O << '%';
517 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
518 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000519 return false;
520}
521
Evan Cheng3d48a902006-04-28 21:19:05 +0000522/// PrintAsmOperand - Print out an operand for an inline asm expression.
523///
524bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
525 unsigned AsmVariant,
526 const char *ExtraCode) {
527 // Does this asm operand have a single letter operand modifier?
528 if (ExtraCode && ExtraCode[0]) {
529 if (ExtraCode[1] != 0) return true; // Unknown modifier.
530
531 switch (ExtraCode[0]) {
532 default: return true; // Unknown modifier.
Chris Lattnerb4828722007-01-25 02:53:24 +0000533 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner0d924992006-10-31 20:12:30 +0000534 printOperand(MI, OpNo, "mem");
535 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000536 case 'b': // Print QImode register
537 case 'h': // Print QImode high register
538 case 'w': // Print HImode register
539 case 'k': // Print SImode register
Chris Lattner14393522007-03-25 02:01:03 +0000540 if (MI->getOperand(OpNo).isReg())
541 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
542 printOperand(MI, OpNo);
543 return false;
Chris Lattner7cd5e072007-03-25 01:44:57 +0000544
545 case 'P': // Don't print @PLT, but do print as memory.
546 printOperand(MI, OpNo, "mem");
547 return false;
Evan Cheng3d48a902006-04-28 21:19:05 +0000548 }
549 }
550
551 printOperand(MI, OpNo);
552 return false;
553}
554
555bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
556 unsigned OpNo,
557 unsigned AsmVariant,
558 const char *ExtraCode) {
559 if (ExtraCode && ExtraCode[0])
560 return true; // Unknown modifier.
561 printMemReference(MI, OpNo);
562 return false;
563}
564
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000565/// printMachineInstruction -- Print out a single X86 LLVM instruction
Dan Gohman8bc49c22007-06-25 15:11:25 +0000566/// MI in AT&T syntax to the current output stream.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000567///
568void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
569 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000570
Evan Cheng8f7f7122006-05-05 05:40:20 +0000571 // See if a truncate instruction can be turned into a nop.
572 switch (MI->getOpcode()) {
573 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000574 case X86::TRUNC_64to32:
575 case X86::TRUNC_64to16:
576 case X86::TRUNC_32to16:
577 case X86::TRUNC_32to8:
578 case X86::TRUNC_16to8:
579 case X86::TRUNC_32_to8:
580 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000581 const MachineOperand &MO0 = MI->getOperand(0);
582 const MachineOperand &MO1 = MI->getOperand(1);
583 unsigned Reg0 = MO0.getReg();
584 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000585 unsigned Opc = MI->getOpcode();
586 if (Opc == X86::TRUNC_64to32)
587 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
588 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000589 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000590 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000591 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000592 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000593 if (Reg0 != Reg1)
594 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000595 break;
596 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000597 case X86::PsMOVZX64rr32:
598 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
599 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000600 }
601
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000602 // Call the autogenerated instruction printer routines.
603 printInstruction(MI);
604}
605
606// Include the auto-generated portion of the assembly writer.
607#include "X86GenAsmWriter.inc"
608