blob: e94bf3a570582cfe52438c45f49c395ec88b4e93 [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) {
Jim Laskeye29c2f52006-07-19 11:54:50 +000029 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
30 // Let PassManager know we need debug information and relay
31 // the MachineDebugInfo address on to DwarfWriter.
32 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
33 }
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 }
77 break;
78 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000079 O << CurrentFnName << ":\n";
80
Evan Cheng932ad512006-05-25 21:59:08 +000081 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +000082 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +000083 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +000084 }
85
Chris Lattnerb36cbd02005-07-01 22:44:09 +000086 // Print out code for the function.
87 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
88 I != E; ++I) {
89 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +000090 if (I->pred_begin() != I->pred_end()) {
91 printBasicBlockLabel(I, true);
92 O << '\n';
93 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000094 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
95 II != E; ++II) {
96 // Print the assembly for the instruction.
97 O << "\t";
98 printMachineInstruction(II);
99 }
100 }
Chris Lattnerac2902b2005-11-21 23:06:54 +0000101 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +0000102 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000103
Evan Cheng932ad512006-05-25 21:59:08 +0000104 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Evan Chengd5948812006-03-07 02:23:26 +0000105 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000106 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000107 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000108
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000109 // We didn't modify anything.
110 return false;
111}
112
Chris Lattnera3b8c572006-02-06 23:41:19 +0000113void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
114 const char *Modifier) {
115 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000116 const MRegisterInfo &RI = *TM.getRegisterInfo();
117 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000118 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000119 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
120 "Virtual registers should not make it this far!");
121 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000122 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000123 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
124 MVT::ValueType VT = (strcmp(Modifier,"subreg16") == 0)
Evan Cheng403be7e2006-05-08 08:01:26 +0000125 ? MVT::i16 : MVT::i8;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000126 Reg = getX86SubSuperRegister(Reg, VT);
127 }
128 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000129 O << (char)tolower(*Name);
130 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000131 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000132
Chris Lattner63b3d712006-05-04 17:21:20 +0000133 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000134 if (!Modifier || strcmp(Modifier, "debug") != 0)
135 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000136 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000137 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000138 case MachineOperand::MO_MachineBasicBlock:
139 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000140 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000141 case MachineOperand::MO_JumpTableIndex: {
142 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
143 if (!isMemOp) O << '$';
144 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
145 << MO.getJumpTableIndex();
146 // FIXME: PIC relocation model
147 return;
148 }
Evan Chenga09bd812006-02-26 08:28:12 +0000149 case MachineOperand::MO_ConstantPoolIndex: {
150 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
151 if (!isMemOp) O << '$';
152 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
153 << MO.getConstantPoolIndex();
Evan Cheng932ad512006-05-25 21:59:08 +0000154 if (Subtarget->TargetType == X86Subtarget::isDarwin &&
155 TM.getRelocationModel() == Reloc::PIC)
Evan Chenga09bd812006-02-26 08:28:12 +0000156 O << "-\"L" << getFunctionNumber() << "$pb\"";
157 int Offset = MO.getOffset();
158 if (Offset > 0)
159 O << "+" << Offset;
160 else if (Offset < 0)
161 O << Offset;
162 return;
163 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000164 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000165 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000167 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000168 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng932ad512006-05-25 21:59:08 +0000169 if (Subtarget->TargetType == X86Subtarget::isDarwin &&
170 TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000171 GlobalValue *GV = MO.getGlobal();
172 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000173 // Link-once, External, or Weakly-linked global variables need
174 // non-lazily-resolved stubs
175 if (GV->isExternal() || GV->hasWeakLinkage() ||
176 GV->hasLinkOnceLinkage()) {
177 // Dynamically-resolved functions need a stub for the function.
178 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
179 FnStubs.insert(Name);
180 O << "L" << Name << "$stub";
181 } else {
182 GVStubs.insert(Name);
183 O << "L" << Name << "$non_lazy_ptr";
184 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000185 } else {
186 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000187 }
188 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
189 O << "-\"L" << getFunctionNumber() << "$pb\"";
190 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000191 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000192 int Offset = MO.getOffset();
193 if (Offset > 0)
194 O << "+" << Offset;
195 else if (Offset < 0)
196 O << Offset;
197 return;
198 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000199 case MachineOperand::MO_ExternalSymbol: {
200 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000201 if (isCallOp &&
202 Subtarget->TargetType == X86Subtarget::isDarwin &&
203 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng4c1aa862006-02-22 20:19:42 +0000204 std::string Name(GlobalPrefix);
205 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000206 FnStubs.insert(Name);
207 O << "L" << Name << "$stub";
208 return;
209 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000210 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000211 O << GlobalPrefix << MO.getSymbolName();
212 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000213 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000214 default:
215 O << "<unknown operand type>"; return;
216 }
217}
218
Nate Begeman391c5d22005-11-30 18:54:35 +0000219void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000220 unsigned char value = MI->getOperand(Op).getImmedValue();
221 assert(value <= 7 && "Invalid ssecc argument!");
222 switch (value) {
223 case 0: O << "eq"; break;
224 case 1: O << "lt"; break;
225 case 2: O << "le"; break;
226 case 3: O << "unord"; break;
227 case 4: O << "neq"; break;
228 case 5: O << "nlt"; break;
229 case 6: O << "nle"; break;
230 case 7: O << "ord"; break;
231 }
232}
233
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000234void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
235 assert(isMem(MI, Op) && "Invalid memory reference!");
236
237 const MachineOperand &BaseReg = MI->getOperand(Op);
238 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
239 const MachineOperand &IndexReg = MI->getOperand(Op+2);
240 const MachineOperand &DispSpec = MI->getOperand(Op+3);
241
242 if (BaseReg.isFrameIndex()) {
243 O << "[frame slot #" << BaseReg.getFrameIndex();
244 if (DispSpec.getImmedValue())
245 O << " + " << DispSpec.getImmedValue();
246 O << "]";
247 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000248 }
249
Evan Chenga09bd812006-02-26 08:28:12 +0000250 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000251 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000252 } else {
253 int DispVal = DispSpec.getImmedValue();
254 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
255 O << DispVal;
256 }
257
258 if (IndexReg.getReg() || BaseReg.getReg()) {
259 O << "(";
260 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000261 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000262
263 if (IndexReg.getReg()) {
264 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000265 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000266 if (ScaleVal != 1)
267 O << "," << ScaleVal;
268 }
269
270 O << ")";
271 }
272}
273
Evan Cheng7ccced62006-02-18 00:15:05 +0000274void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
275 O << "\"L" << getFunctionNumber() << "$pb\"\n";
276 O << "\"L" << getFunctionNumber() << "$pb\":";
277}
278
Evan Cheng62f27002006-04-28 23:11:40 +0000279
Evan Cheng55c25f22006-04-28 23:19:39 +0000280bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000281 const char Mode) {
282 const MRegisterInfo &RI = *TM.getRegisterInfo();
283 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000284 switch (Mode) {
285 default: return true; // Unknown mode.
286 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000287 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000288 break;
289 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000290 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000291 break;
292 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000293 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000294 break;
295 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000296 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000297 break;
298 }
299
Evan Cheng8f7f7122006-05-05 05:40:20 +0000300 O << '%';
301 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
302 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000303 return false;
304}
305
Evan Cheng3d48a902006-04-28 21:19:05 +0000306/// PrintAsmOperand - Print out an operand for an inline asm expression.
307///
308bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
309 unsigned AsmVariant,
310 const char *ExtraCode) {
311 // Does this asm operand have a single letter operand modifier?
312 if (ExtraCode && ExtraCode[0]) {
313 if (ExtraCode[1] != 0) return true; // Unknown modifier.
314
315 switch (ExtraCode[0]) {
316 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000317 case 'b': // Print QImode register
318 case 'h': // Print QImode high register
319 case 'w': // Print HImode register
320 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000321 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000322 }
323 }
324
325 printOperand(MI, OpNo);
326 return false;
327}
328
329bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
330 unsigned OpNo,
331 unsigned AsmVariant,
332 const char *ExtraCode) {
333 if (ExtraCode && ExtraCode[0])
334 return true; // Unknown modifier.
335 printMemReference(MI, OpNo);
336 return false;
337}
338
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000339/// printMachineInstruction -- Print out a single X86 LLVM instruction
340/// MI in Intel syntax to the current output stream.
341///
342void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
343 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000344 // This works around some Darwin assembler bugs.
Evan Cheng932ad512006-05-25 21:59:08 +0000345 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Evan Cheng67caa392006-01-26 02:27:43 +0000346 switch (MI->getOpcode()) {
347 case X86::REP_MOVSB:
348 O << "rep/movsb (%esi),(%edi)\n";
349 return;
350 case X86::REP_MOVSD:
351 O << "rep/movsl (%esi),(%edi)\n";
352 return;
353 case X86::REP_MOVSW:
354 O << "rep/movsw (%esi),(%edi)\n";
355 return;
356 case X86::REP_STOSB:
357 O << "rep/stosb\n";
358 return;
359 case X86::REP_STOSD:
360 O << "rep/stosl\n";
361 return;
362 case X86::REP_STOSW:
363 O << "rep/stosw\n";
364 return;
365 default:
366 break;
367 }
368 }
369
Evan Cheng8f7f7122006-05-05 05:40:20 +0000370 // See if a truncate instruction can be turned into a nop.
371 switch (MI->getOpcode()) {
372 default: break;
Evan Cheng069287d2006-05-16 07:21:53 +0000373 case X86::TRUNC_GR32_GR16:
374 case X86::TRUNC_GR32_GR8:
375 case X86::TRUNC_GR16_GR8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000376 const MachineOperand &MO0 = MI->getOperand(0);
377 const MachineOperand &MO1 = MI->getOperand(1);
378 unsigned Reg0 = MO0.getReg();
379 unsigned Reg1 = MO1.getReg();
Evan Cheng069287d2006-05-16 07:21:53 +0000380 if (MI->getOpcode() == X86::TRUNC_GR32_GR16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000381 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000382 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000383 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
384 O << CommentString << " TRUNCATE ";
385 if (Reg0 != Reg1)
386 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000387 break;
388 }
389 }
390
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000391 // Call the autogenerated instruction printer routines.
392 printInstruction(MI);
393}
394
395// Include the auto-generated portion of the assembly writer.
396#include "X86GenAsmWriter.inc"
397