blob: 7edea6f07453b55b6cf04b979d2c9d2020ab36b4 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly ----===//
2//
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 Korobeynikovf8248682006-09-20 22:03:51 +000019#include "X86MachineFunctionInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000020#include "X86TargetMachine.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000021#include "X86TargetAsmInfo.h"
Anton Korobeynikov7f705592007-01-12 19:20:47 +000022#include "llvm/ADT/StringExtras.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000023#include "llvm/CallingConv.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000024#include "llvm/Module.h"
25#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattner95b2c7d2006-12-19 22:59:26 +000028#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000029using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030
Chris Lattner95b2c7d2006-12-19 22:59:26 +000031STATISTIC(EmittedInsts, "Number of machine instrs printed");
32
Anton Korobeynikov7f705592007-01-12 19:20:47 +000033static std::string computePICLabel(unsigned fnNumber,
34 const X86Subtarget* Subtarget)
35{
36 std::string label;
37
38 if (Subtarget->isTargetDarwin()) {
39 label = "\"L" + utostr_32(fnNumber) + "$pb\"";
40 } else if (Subtarget->isTargetELF()) {
41 label = ".Lllvm$" + utostr_32(fnNumber) + "$piclabel";
42 } else
43 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()) {
Anton Korobeynikovb1c88022006-10-18 09:12:29 +000062 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000063 } else {
64 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
65 ",\"ax\",@progbits\n";
66 }
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 Korobeynikovab4022f2006-10-31 08:31:24 +000074 if (Subtarget->isTargetDarwin() ||
75 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +000076 Subtarget->isTargetCygMing()) {
Jim Laskeye29c2f52006-07-19 11:54:50 +000077 // Let PassManager know we need debug information and relay
78 // the MachineDebugInfo address on to DwarfWriter.
79 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
80 }
Evan Cheng3c992d22006-03-07 02:02:57 +000081
Chris Lattner8b8b9512005-11-21 07:51:23 +000082 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000083 O << "\n\n";
84
85 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000086 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000087
88 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000089 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000090 unsigned CC = F->getCallingConv();
91
92 // Populate function information map. Actually, We don't want to populate
93 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000094 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
95 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000096
97 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
98
Anton Korobeynikovd5f317d2007-01-07 00:41:20 +000099 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
Anton Korobeynikovcea9d1d2007-01-06 18:24:26 +0000100
Evan Cheng2338c5c2006-02-07 08:38:37 +0000101 switch (F->getLinkage()) {
102 default: assert(0 && "Unknown linkage type!");
103 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2338c5c2006-02-07 08:38:37 +0000104 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
105 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000106 case Function::DLLExportLinkage:
107 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
108 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +0000109 case Function::ExternalLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +0000110 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000111 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000112 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000113 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000114 case Function::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +0000115 if (Subtarget->isTargetDarwin()) {
Evan Chengf1616da2006-02-22 23:59:57 +0000116 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000117 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000118 } else if (Subtarget->isTargetCygMing()) {
Evan Cheng932ad512006-05-25 21:59:08 +0000119 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000120 O << "\t.linkonce discard\n";
121 O << "\t.globl " << CurrentFnName << "\n";
122 } else {
123 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
124 O << "\t.weak " << CurrentFnName << "\n";
125 }
126 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000127 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000128 if (F->hasHiddenVisibility())
129 O << "\t.hidden " << CurrentFnName << "\n";
130
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000131 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000132 // Add some workaround for linkonce linkage on Cygwin\MinGW
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000133 if (Subtarget->isTargetCygMing() &&
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000134 (F->getLinkage() == Function::LinkOnceLinkage ||
135 F->getLinkage() == Function::WeakLinkage))
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000136 O << "Lllvm$workaround$fake$stub$" << CurrentFnName << ":\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000137
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000138 if (Subtarget->isTargetDarwin() ||
139 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000140 Subtarget->isTargetCygMing()) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000141 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000142 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000143 }
144
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000145 // Print out code for the function.
146 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
147 I != E; ++I) {
148 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000149 if (I->pred_begin() != I->pred_end()) {
150 printBasicBlockLabel(I, true);
151 O << '\n';
152 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000153 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
154 II != E; ++II) {
155 // Print the assembly for the instruction.
156 O << "\t";
157 printMachineInstruction(II);
158 }
159 }
Evan Cheng67afece2006-08-28 22:14:16 +0000160
Chris Lattner1da31ee2006-10-05 03:01:21 +0000161 // Print out jump tables referenced by the function.
162
163 // Mac OS X requires that the jump table follow the function, so that the jump
164 // table is part of the same atom that the function is in.
165 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Evan Cheng67afece2006-08-28 22:14:16 +0000166
Jim Laskey563321a2006-09-06 18:34:40 +0000167 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000168 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000169
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000170 if (Subtarget->isTargetDarwin() ||
171 Subtarget->isTargetELF() ||
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000172 Subtarget->isTargetCygMing()) {
Evan Chengd5948812006-03-07 02:23:26 +0000173 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000174 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000175 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000176
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000177 // We didn't modify anything.
178 return false;
179}
180
Chris Lattnera3b8c572006-02-06 23:41:19 +0000181void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
Evan Cheng28b514392006-12-05 19:50:18 +0000182 const char *Modifier, bool NotRIPRel) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000183 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000184 const MRegisterInfo &RI = *TM.getRegisterInfo();
185 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000186 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000187 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
188 "Virtual registers should not make it this far!");
189 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000190 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000191 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
193 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
194 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000195 Reg = getX86SubSuperRegister(Reg, VT);
196 }
197 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000198 O << (char)tolower(*Name);
199 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000200 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000201
Chris Lattner63b3d712006-05-04 17:21:20 +0000202 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000203 if (!Modifier || strcmp(Modifier, "debug") != 0)
204 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000205 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000206 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000207 case MachineOperand::MO_MachineBasicBlock:
208 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000210 case MachineOperand::MO_JumpTableIndex: {
211 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
212 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000213 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000214 << MO.getJumpTableIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000215
216 if (TM.getRelocationModel() == Reloc::PIC_) {
217 if (Subtarget->isPICStyleStub())
218 O << "-\"L" << getFunctionNumber() << "$pb\"";
219 else if (Subtarget->isPICStyleGOT())
220 O << "@GOTOFF";
221 }
222
Evan Cheng28b514392006-12-05 19:50:18 +0000223 if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000225 return;
226 }
Evan Chenga09bd812006-02-26 08:28:12 +0000227 case MachineOperand::MO_ConstantPoolIndex: {
228 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
229 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000230 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000231 << MO.getConstantPoolIndex();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000232
233 if (TM.getRelocationModel() == Reloc::PIC_) {
234 if (Subtarget->isPICStyleStub())
235 O << "-\"L" << getFunctionNumber() << "$pb\"";
236 if (Subtarget->isPICStyleGOT())
237 O << "@GOTOFF";
238 }
239
Evan Chenga09bd812006-02-26 08:28:12 +0000240 int Offset = MO.getOffset();
241 if (Offset > 0)
242 O << "+" << Offset;
243 else if (Offset < 0)
244 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000245
Evan Cheng28b514392006-12-05 19:50:18 +0000246 if (isMemOp && Subtarget->is64Bit() && !NotRIPRel)
Evan Cheng25ab6902006-09-08 06:48:29 +0000247 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000248 return;
249 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000250 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000251 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000252 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000253 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng25ab6902006-09-08 06:48:29 +0000254
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000255 GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000257
Evan Cheng25ab6902006-09-08 06:48:29 +0000258 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
259 GV->hasLinkOnceLinkage());
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000260 bool isHidden = GV->hasHiddenVisibility();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000261
Chris Lattnere87e1152006-09-26 03:57:53 +0000262 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000263
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000264 if (Subtarget->isPICStyleStub()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000265 // Link-once, External, or Weakly-linked global variables need
266 // non-lazily-resolved stubs
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 if (isExt) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000268 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000270 FnStubs.insert(Name);
271 O << "L" << Name << "$stub";
272 } else {
273 GVStubs.insert(Name);
274 O << "L" << Name << "$non_lazy_ptr";
275 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000276 } else {
277 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000278 O << "__imp_";
279 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000280 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000281 }
282
Chris Lattner35d86fe2006-07-26 21:12:04 +0000283 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga0ea0532006-02-23 02:43:52 +0000284 O << "-\"L" << getFunctionNumber() << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000285 } else {
286 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000287 O << "__imp_";
288 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 O << Name;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000290
291 if (Subtarget->isPICStyleGOT() && isCallOp && isa<Function>(GV)) {
292 // Assemble call via PLT for non-local symbols
293 if (!isHidden || isExt)
294 O << "@PLT";
295 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000296 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000297
Evan Cheng6d7d3102006-12-01 07:38:23 +0000298 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000299 ExtWeakSymbols.insert(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000300
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000301 int Offset = MO.getOffset();
302 if (Offset > 0)
303 O << "+" << Offset;
304 else if (Offset < 0)
305 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000306
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000307 if (isMemOp) {
308 if (isExt) {
309 if (Subtarget->isPICStyleGOT()) {
310 O << "@GOT";
311 } else if (Subtarget->isPICStyleRIPRel()) {
312 O << "@GOTPCREL(%rip)";
313 } if (Subtarget->is64Bit() && !NotRIPRel)
314 // Use rip when possible to reduce code size, except when
315 // index or base register are also part of the address. e.g.
316 // foo(%rip)(%rcx,%rax,4) is not legal
317 O << "(%rip)";
318 } else {
319 if (Subtarget->is64Bit() && !NotRIPRel)
320 O << "(%rip)";
321 else if (Subtarget->isPICStyleGOT())
322 O << "@GOTOFF";
323 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
325
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000326 return;
327 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000328 case MachineOperand::MO_ExternalSymbol: {
329 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000330 std::string Name(TAI->getGlobalPrefix());
331 Name += MO.getSymbolName();
332 if (isCallOp && Subtarget->isPICStyleStub()) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000333 FnStubs.insert(Name);
334 O << "L" << Name << "$stub";
335 return;
336 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000337 if (!isCallOp) O << '$';
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000338 O << Name;
339
340 if (Subtarget->isPICStyleGOT()) {
341 std::string GOTName(TAI->getGlobalPrefix());
342 GOTName+="_GLOBAL_OFFSET_TABLE_";
343 if (Name == GOTName)
344 // Really hack! Emit extra offset to PC during printing GOT offset to
345 // compensate size of popl instruction. The resulting code should look
346 // like:
347 // call .piclabel
348 // piclabel:
349 // popl %some_register
350 // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
351 O << " + [.-" << computePICLabel(getFunctionNumber(), Subtarget) << "]";
352 }
353
354 if (isCallOp && Subtarget->isPICStyleGOT())
355 O << "@PLT";
Evan Cheng25ab6902006-09-08 06:48:29 +0000356
Evan Cheng35c1c042006-12-05 06:43:58 +0000357 if (!isCallOp && Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000358 O << "(%rip)";
359
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000360 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000361 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000362 default:
363 O << "<unknown operand type>"; return;
364 }
365}
366
Nate Begeman391c5d22005-11-30 18:54:35 +0000367void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000368 unsigned char value = MI->getOperand(Op).getImmedValue();
369 assert(value <= 7 && "Invalid ssecc argument!");
370 switch (value) {
371 case 0: O << "eq"; break;
372 case 1: O << "lt"; break;
373 case 2: O << "le"; break;
374 case 3: O << "unord"; break;
375 case 4: O << "neq"; break;
376 case 5: O << "nlt"; break;
377 case 6: O << "nle"; break;
378 case 7: O << "ord"; break;
379 }
380}
381
Evan Cheng25ab6902006-09-08 06:48:29 +0000382void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
383 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000384 assert(isMem(MI, Op) && "Invalid memory reference!");
385
386 const MachineOperand &BaseReg = MI->getOperand(Op);
387 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
388 const MachineOperand &IndexReg = MI->getOperand(Op+2);
389 const MachineOperand &DispSpec = MI->getOperand(Op+3);
390
391 if (BaseReg.isFrameIndex()) {
392 O << "[frame slot #" << BaseReg.getFrameIndex();
393 if (DispSpec.getImmedValue())
394 O << " + " << DispSpec.getImmedValue();
395 O << "]";
396 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000397 }
398
Evan Cheng28b514392006-12-05 19:50:18 +0000399 bool NotRIPRel = IndexReg.getReg() || BaseReg.getReg();
Evan Chengc9676de2006-08-29 22:14:48 +0000400 if (DispSpec.isGlobalAddress() ||
401 DispSpec.isConstantPoolIndex() ||
402 DispSpec.isJumpTableIndex()) {
Evan Cheng28b514392006-12-05 19:50:18 +0000403 printOperand(MI, Op+3, "mem", NotRIPRel);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000404 } else {
405 int DispVal = DispSpec.getImmedValue();
406 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
407 O << DispVal;
408 }
409
410 if (IndexReg.getReg() || BaseReg.getReg()) {
411 O << "(";
Evan Cheng25ab6902006-09-08 06:48:29 +0000412 if (BaseReg.getReg()) {
413 printOperand(MI, Op, Modifier);
414 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000415
416 if (IndexReg.getReg()) {
417 O << ",";
Evan Cheng25ab6902006-09-08 06:48:29 +0000418 printOperand(MI, Op+2, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000419 if (ScaleVal != 1)
420 O << "," << ScaleVal;
421 }
422
423 O << ")";
424 }
425}
426
Evan Cheng7ccced62006-02-18 00:15:05 +0000427void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000428 std::string label = computePICLabel(getFunctionNumber(), Subtarget);
429 O << label << "\n" << label << ":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000430}
431
Evan Cheng62f27002006-04-28 23:11:40 +0000432
Evan Cheng55c25f22006-04-28 23:19:39 +0000433bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000434 const char Mode) {
435 const MRegisterInfo &RI = *TM.getRegisterInfo();
436 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000437 switch (Mode) {
438 default: return true; // Unknown mode.
439 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000440 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000441 break;
442 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000443 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000444 break;
445 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000446 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000447 break;
448 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000449 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000450 break;
451 }
452
Evan Cheng8f7f7122006-05-05 05:40:20 +0000453 O << '%';
454 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
455 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000456 return false;
457}
458
Evan Cheng3d48a902006-04-28 21:19:05 +0000459/// PrintAsmOperand - Print out an operand for an inline asm expression.
460///
461bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
462 unsigned AsmVariant,
463 const char *ExtraCode) {
464 // Does this asm operand have a single letter operand modifier?
465 if (ExtraCode && ExtraCode[0]) {
466 if (ExtraCode[1] != 0) return true; // Unknown modifier.
467
468 switch (ExtraCode[0]) {
469 default: return true; // Unknown modifier.
Chris Lattner0d924992006-10-31 20:12:30 +0000470 case 'c': // Don't print "$" before a global var name.
471 printOperand(MI, OpNo, "mem");
472 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000473 case 'b': // Print QImode register
474 case 'h': // Print QImode high register
475 case 'w': // Print HImode register
476 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000477 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000478 }
479 }
480
481 printOperand(MI, OpNo);
482 return false;
483}
484
485bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
486 unsigned OpNo,
487 unsigned AsmVariant,
488 const char *ExtraCode) {
489 if (ExtraCode && ExtraCode[0])
490 return true; // Unknown modifier.
491 printMemReference(MI, OpNo);
492 return false;
493}
494
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000495/// printMachineInstruction -- Print out a single X86 LLVM instruction
496/// MI in Intel syntax to the current output stream.
497///
498void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
499 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000500
Evan Cheng8f7f7122006-05-05 05:40:20 +0000501 // See if a truncate instruction can be turned into a nop.
502 switch (MI->getOpcode()) {
503 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000504 case X86::TRUNC_64to32:
505 case X86::TRUNC_64to16:
506 case X86::TRUNC_32to16:
507 case X86::TRUNC_32to8:
508 case X86::TRUNC_16to8:
509 case X86::TRUNC_32_to8:
510 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000511 const MachineOperand &MO0 = MI->getOperand(0);
512 const MachineOperand &MO1 = MI->getOperand(1);
513 unsigned Reg0 = MO0.getReg();
514 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000515 unsigned Opc = MI->getOpcode();
516 if (Opc == X86::TRUNC_64to32)
517 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
518 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000519 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000520 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000521 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000522 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000523 if (Reg0 != Reg1)
524 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000525 break;
526 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000527 case X86::PsMOVZX64rr32:
528 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
529 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000530 }
531
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000532 // Call the autogenerated instruction printer routines.
533 printInstruction(MI);
534}
535
536// Include the auto-generated portion of the assembly writer.
537#include "X86GenAsmWriter.inc"
538