blob: f103bccd318f2373772cf868a4510d2f8d77d79d [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
16#include "X86ATTAsmPrinter.h"
17#include "X86.h"
18#include "X86TargetMachine.h"
19#include "llvm/Module.h"
20#include "llvm/Support/Mangler.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000022#include <iostream>
Chris Lattnerb36cbd02005-07-01 22:44:09 +000023using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000024
25/// runOnMachineFunction - This uses the printMachineInstruction()
26/// method to print assembly for each instruction.
27///
28bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng932ad512006-05-25 21:59:08 +000029 // Let PassManager know we need debug information and relay
30 // the MachineDebugInfo address on to DwarfWriter.
31 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
Jim Laskey014f98c2006-06-14 11:35:03 +000032 // FIXME - should be able to debug coalesced functions.
33 bool IsNormalText = true;
Evan Cheng3c992d22006-03-07 02:02:57 +000034
Chris Lattner8b8b9512005-11-21 07:51:23 +000035 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000036 O << "\n\n";
37
38 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000039 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000040
Nate Begeman37efe672006-04-22 18:53:45 +000041 // Print out jump tables referenced by the function
42 EmitJumpTableInfo(MF.getJumpTableInfo());
43
Chris Lattnerb36cbd02005-07-01 22:44:09 +000044 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000045 const Function *F = MF.getFunction();
46 switch (F->getLinkage()) {
47 default: assert(0 && "Unknown linkage type!");
48 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattnerdad9c5a2006-05-09 05:12:53 +000049 SwitchToTextSection(DefaultTextSection, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000050 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
51 break;
52 case Function::ExternalLinkage:
Chris Lattnerdad9c5a2006-05-09 05:12:53 +000053 SwitchToTextSection(DefaultTextSection, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000054 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Chris Lattner272f9982005-12-16 00:07:30 +000055 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000056 break;
57 case Function::WeakLinkage:
58 case Function::LinkOnceLinkage:
Evan Cheng932ad512006-05-25 21:59:08 +000059 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Chris Lattner4632d7a2006-05-09 04:59:56 +000060 SwitchToTextSection(
61 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
Evan Chengf1616da2006-02-22 23:59:57 +000062 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000063 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Evan Cheng932ad512006-05-25 21:59:08 +000064 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
65 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
66 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
67 << ",\"ax\"\n";
68 SwitchToTextSection("", F);
69 O << "\t.weak " << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000070 } else {
71 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
72 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
73 << ",\"ax\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +000074 SwitchToTextSection("", F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000075 O << "\t.weak " << CurrentFnName << "\n";
76 }
Jim Laskey014f98c2006-06-14 11:35:03 +000077 IsNormalText = false;
Evan Cheng2338c5c2006-02-07 08:38:37 +000078 break;
79 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000080 O << CurrentFnName << ":\n";
81
Evan Cheng932ad512006-05-25 21:59:08 +000082 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +000083 // Emit pre-function debug information.
Jim Laskey014f98c2006-06-14 11:35:03 +000084 DW.BeginFunction(&MF, IsNormalText);
Jim Laskey6b92b8e2006-04-07 20:44:42 +000085 }
86
Chris Lattnerb36cbd02005-07-01 22:44:09 +000087 // Print out code for the function.
88 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
89 I != E; ++I) {
90 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +000091 if (I->pred_begin() != I->pred_end()) {
92 printBasicBlockLabel(I, true);
93 O << '\n';
94 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000095 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
96 II != E; ++II) {
97 // Print the assembly for the instruction.
98 O << "\t";
99 printMachineInstruction(II);
100 }
101 }
Chris Lattnerac2902b2005-11-21 23:06:54 +0000102 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +0000103 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000104
Evan Cheng932ad512006-05-25 21:59:08 +0000105 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Evan Chengd5948812006-03-07 02:23:26 +0000106 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000107 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000108 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000109
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000110 // We didn't modify anything.
111 return false;
112}
113
Chris Lattnera3b8c572006-02-06 23:41:19 +0000114void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
115 const char *Modifier) {
116 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000117 const MRegisterInfo &RI = *TM.getRegisterInfo();
118 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000119 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000120 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
121 "Virtual registers should not make it this far!");
122 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000123 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000124 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
125 MVT::ValueType VT = (strcmp(Modifier,"subreg16") == 0)
Evan Cheng403be7e2006-05-08 08:01:26 +0000126 ? MVT::i16 : MVT::i8;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000127 Reg = getX86SubSuperRegister(Reg, VT);
128 }
129 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000130 O << (char)tolower(*Name);
131 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000132 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000133
Chris Lattner63b3d712006-05-04 17:21:20 +0000134 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000135 if (!Modifier || strcmp(Modifier, "debug") != 0)
136 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000137 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000138 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000139 case MachineOperand::MO_MachineBasicBlock:
140 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000141 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000142 case MachineOperand::MO_JumpTableIndex: {
143 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
144 if (!isMemOp) O << '$';
145 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
146 << MO.getJumpTableIndex();
147 // FIXME: PIC relocation model
148 return;
149 }
Evan Chenga09bd812006-02-26 08:28:12 +0000150 case MachineOperand::MO_ConstantPoolIndex: {
151 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
152 if (!isMemOp) O << '$';
153 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
154 << MO.getConstantPoolIndex();
Evan Cheng932ad512006-05-25 21:59:08 +0000155 if (Subtarget->TargetType == X86Subtarget::isDarwin &&
156 TM.getRelocationModel() == Reloc::PIC)
Evan Chenga09bd812006-02-26 08:28:12 +0000157 O << "-\"L" << getFunctionNumber() << "$pb\"";
158 int Offset = MO.getOffset();
159 if (Offset > 0)
160 O << "+" << Offset;
161 else if (Offset < 0)
162 O << Offset;
163 return;
164 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000165 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000166 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000167 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000168 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000169 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng932ad512006-05-25 21:59:08 +0000170 if (Subtarget->TargetType == X86Subtarget::isDarwin &&
171 TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000172 GlobalValue *GV = MO.getGlobal();
173 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000174 // Link-once, External, or Weakly-linked global variables need
175 // non-lazily-resolved stubs
176 if (GV->isExternal() || GV->hasWeakLinkage() ||
177 GV->hasLinkOnceLinkage()) {
178 // Dynamically-resolved functions need a stub for the function.
179 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
180 FnStubs.insert(Name);
181 O << "L" << Name << "$stub";
182 } else {
183 GVStubs.insert(Name);
184 O << "L" << Name << "$non_lazy_ptr";
185 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000186 } else {
187 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000188 }
189 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
190 O << "-\"L" << getFunctionNumber() << "$pb\"";
191 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000192 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000193 int Offset = MO.getOffset();
194 if (Offset > 0)
195 O << "+" << Offset;
196 else if (Offset < 0)
197 O << Offset;
198 return;
199 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000200 case MachineOperand::MO_ExternalSymbol: {
201 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000202 if (isCallOp &&
203 Subtarget->TargetType == X86Subtarget::isDarwin &&
204 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng4c1aa862006-02-22 20:19:42 +0000205 std::string Name(GlobalPrefix);
206 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000207 FnStubs.insert(Name);
208 O << "L" << Name << "$stub";
209 return;
210 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000211 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000212 O << GlobalPrefix << MO.getSymbolName();
213 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000214 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000215 default:
216 O << "<unknown operand type>"; return;
217 }
218}
219
Nate Begeman391c5d22005-11-30 18:54:35 +0000220void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000221 unsigned char value = MI->getOperand(Op).getImmedValue();
222 assert(value <= 7 && "Invalid ssecc argument!");
223 switch (value) {
224 case 0: O << "eq"; break;
225 case 1: O << "lt"; break;
226 case 2: O << "le"; break;
227 case 3: O << "unord"; break;
228 case 4: O << "neq"; break;
229 case 5: O << "nlt"; break;
230 case 6: O << "nle"; break;
231 case 7: O << "ord"; break;
232 }
233}
234
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000235void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
236 assert(isMem(MI, Op) && "Invalid memory reference!");
237
238 const MachineOperand &BaseReg = MI->getOperand(Op);
239 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
240 const MachineOperand &IndexReg = MI->getOperand(Op+2);
241 const MachineOperand &DispSpec = MI->getOperand(Op+3);
242
243 if (BaseReg.isFrameIndex()) {
244 O << "[frame slot #" << BaseReg.getFrameIndex();
245 if (DispSpec.getImmedValue())
246 O << " + " << DispSpec.getImmedValue();
247 O << "]";
248 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000249 }
250
Evan Chenga09bd812006-02-26 08:28:12 +0000251 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000252 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000253 } else {
254 int DispVal = DispSpec.getImmedValue();
255 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
256 O << DispVal;
257 }
258
259 if (IndexReg.getReg() || BaseReg.getReg()) {
260 O << "(";
261 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000262 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000263
264 if (IndexReg.getReg()) {
265 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000266 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000267 if (ScaleVal != 1)
268 O << "," << ScaleVal;
269 }
270
271 O << ")";
272 }
273}
274
Evan Cheng7ccced62006-02-18 00:15:05 +0000275void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
276 O << "\"L" << getFunctionNumber() << "$pb\"\n";
277 O << "\"L" << getFunctionNumber() << "$pb\":";
278}
279
Evan Cheng62f27002006-04-28 23:11:40 +0000280
Evan Cheng55c25f22006-04-28 23:19:39 +0000281bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000282 const char Mode) {
283 const MRegisterInfo &RI = *TM.getRegisterInfo();
284 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000285 switch (Mode) {
286 default: return true; // Unknown mode.
287 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000288 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000289 break;
290 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000291 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000292 break;
293 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000294 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000295 break;
296 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000297 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000298 break;
299 }
300
Evan Cheng8f7f7122006-05-05 05:40:20 +0000301 O << '%';
302 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
303 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000304 return false;
305}
306
Evan Cheng3d48a902006-04-28 21:19:05 +0000307/// PrintAsmOperand - Print out an operand for an inline asm expression.
308///
309bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
310 unsigned AsmVariant,
311 const char *ExtraCode) {
312 // Does this asm operand have a single letter operand modifier?
313 if (ExtraCode && ExtraCode[0]) {
314 if (ExtraCode[1] != 0) return true; // Unknown modifier.
315
316 switch (ExtraCode[0]) {
317 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000318 case 'b': // Print QImode register
319 case 'h': // Print QImode high register
320 case 'w': // Print HImode register
321 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000322 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000323 }
324 }
325
326 printOperand(MI, OpNo);
327 return false;
328}
329
330bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
331 unsigned OpNo,
332 unsigned AsmVariant,
333 const char *ExtraCode) {
334 if (ExtraCode && ExtraCode[0])
335 return true; // Unknown modifier.
336 printMemReference(MI, OpNo);
337 return false;
338}
339
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000340/// printMachineInstruction -- Print out a single X86 LLVM instruction
341/// MI in Intel syntax to the current output stream.
342///
343void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
344 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000345 // This works around some Darwin assembler bugs.
Evan Cheng932ad512006-05-25 21:59:08 +0000346 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Evan Cheng67caa392006-01-26 02:27:43 +0000347 switch (MI->getOpcode()) {
348 case X86::REP_MOVSB:
349 O << "rep/movsb (%esi),(%edi)\n";
350 return;
351 case X86::REP_MOVSD:
352 O << "rep/movsl (%esi),(%edi)\n";
353 return;
354 case X86::REP_MOVSW:
355 O << "rep/movsw (%esi),(%edi)\n";
356 return;
357 case X86::REP_STOSB:
358 O << "rep/stosb\n";
359 return;
360 case X86::REP_STOSD:
361 O << "rep/stosl\n";
362 return;
363 case X86::REP_STOSW:
364 O << "rep/stosw\n";
365 return;
366 default:
367 break;
368 }
369 }
370
Evan Cheng8f7f7122006-05-05 05:40:20 +0000371 // See if a truncate instruction can be turned into a nop.
372 switch (MI->getOpcode()) {
373 default: break;
Evan Cheng069287d2006-05-16 07:21:53 +0000374 case X86::TRUNC_GR32_GR16:
375 case X86::TRUNC_GR32_GR8:
376 case X86::TRUNC_GR16_GR8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000377 const MachineOperand &MO0 = MI->getOperand(0);
378 const MachineOperand &MO1 = MI->getOperand(1);
379 unsigned Reg0 = MO0.getReg();
380 unsigned Reg1 = MO1.getReg();
Evan Cheng069287d2006-05-16 07:21:53 +0000381 if (MI->getOpcode() == X86::TRUNC_GR32_GR16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000382 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000383 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000384 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
385 O << CommentString << " TRUNCATE ";
386 if (Reg0 != Reg1)
387 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000388 break;
389 }
390 }
391
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000392 // Call the autogenerated instruction printer routines.
393 printInstruction(MI);
394}
395
396// Include the auto-generated portion of the assembly writer.
397#include "X86GenAsmWriter.inc"
398