blob: 7d40c8842aedc94c0c813678951d41dde03f1788 [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 Chengd5948812006-03-07 02:23:26 +000029 // if (forDarwin) {
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:
59 if (forDarwin) {
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";
64 } else {
65 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
66 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
67 << ",\"ax\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +000068 SwitchToTextSection("", F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000069 O << "\t.weak " << CurrentFnName << "\n";
70 }
71 break;
72 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000073 O << CurrentFnName << ":\n";
74
Jim Laskey6b92b8e2006-04-07 20:44:42 +000075 if (forDarwin) {
76 // Emit pre-function debug information.
77 DW.BeginFunction(&MF);
78 }
79
Chris Lattnerb36cbd02005-07-01 22:44:09 +000080 // Print out code for the function.
81 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
82 I != E; ++I) {
83 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +000084 if (I->pred_begin() != I->pred_end()) {
85 printBasicBlockLabel(I, true);
86 O << '\n';
87 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000088 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
89 II != E; ++II) {
90 // Print the assembly for the instruction.
91 O << "\t";
92 printMachineInstruction(II);
93 }
94 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000095 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000096 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000097
Evan Chengd5948812006-03-07 02:23:26 +000098 if (forDarwin) {
99 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000100 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000101 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000102
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000103 // We didn't modify anything.
104 return false;
105}
106
Chris Lattnera3b8c572006-02-06 23:41:19 +0000107void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
108 const char *Modifier) {
109 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000110 const MRegisterInfo &RI = *TM.getRegisterInfo();
111 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000112 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000113 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
114 "Virtual registers should not make it this far!");
115 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000116 unsigned Reg = MO.getReg();
117 if (Modifier && strncmp(Modifier, "trunc", strlen("trunc")) == 0) {
118 MVT::ValueType VT = (strcmp(Modifier,"trunc16") == 0)
Evan Cheng403be7e2006-05-08 08:01:26 +0000119 ? MVT::i16 : MVT::i8;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000120 Reg = getX86SubSuperRegister(Reg, VT);
121 }
122 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000123 O << (char)tolower(*Name);
124 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000125 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000126
Chris Lattner63b3d712006-05-04 17:21:20 +0000127 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000128 if (!Modifier || strcmp(Modifier, "debug") != 0)
129 O << '$';
130 O << (int)MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000131 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000132 case MachineOperand::MO_MachineBasicBlock:
133 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000134 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000135 case MachineOperand::MO_JumpTableIndex: {
136 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
137 if (!isMemOp) O << '$';
138 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
139 << MO.getJumpTableIndex();
140 // FIXME: PIC relocation model
141 return;
142 }
Evan Chenga09bd812006-02-26 08:28:12 +0000143 case MachineOperand::MO_ConstantPoolIndex: {
144 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
145 if (!isMemOp) O << '$';
146 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
147 << MO.getConstantPoolIndex();
148 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
149 O << "-\"L" << getFunctionNumber() << "$pb\"";
150 int Offset = MO.getOffset();
151 if (Offset > 0)
152 O << "+" << Offset;
153 else if (Offset < 0)
154 O << Offset;
155 return;
156 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000157 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000158 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000159 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000160 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000161 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000162 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000163 GlobalValue *GV = MO.getGlobal();
164 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000165 // Link-once, External, or Weakly-linked global variables need
166 // non-lazily-resolved stubs
167 if (GV->isExternal() || GV->hasWeakLinkage() ||
168 GV->hasLinkOnceLinkage()) {
169 // Dynamically-resolved functions need a stub for the function.
170 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
171 FnStubs.insert(Name);
172 O << "L" << Name << "$stub";
173 } else {
174 GVStubs.insert(Name);
175 O << "L" << Name << "$non_lazy_ptr";
176 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000177 } else {
178 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000179 }
180 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
181 O << "-\"L" << getFunctionNumber() << "$pb\"";
182 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000183 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000184 int Offset = MO.getOffset();
185 if (Offset > 0)
186 O << "+" << Offset;
187 else if (Offset < 0)
188 O << Offset;
189 return;
190 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000191 case MachineOperand::MO_ExternalSymbol: {
192 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000193 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
194 std::string Name(GlobalPrefix);
195 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000196 FnStubs.insert(Name);
197 O << "L" << Name << "$stub";
198 return;
199 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000200 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000201 O << GlobalPrefix << MO.getSymbolName();
202 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000203 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000204 default:
205 O << "<unknown operand type>"; return;
206 }
207}
208
Nate Begeman391c5d22005-11-30 18:54:35 +0000209void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000210 unsigned char value = MI->getOperand(Op).getImmedValue();
211 assert(value <= 7 && "Invalid ssecc argument!");
212 switch (value) {
213 case 0: O << "eq"; break;
214 case 1: O << "lt"; break;
215 case 2: O << "le"; break;
216 case 3: O << "unord"; break;
217 case 4: O << "neq"; break;
218 case 5: O << "nlt"; break;
219 case 6: O << "nle"; break;
220 case 7: O << "ord"; break;
221 }
222}
223
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000224void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
225 assert(isMem(MI, Op) && "Invalid memory reference!");
226
227 const MachineOperand &BaseReg = MI->getOperand(Op);
228 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
229 const MachineOperand &IndexReg = MI->getOperand(Op+2);
230 const MachineOperand &DispSpec = MI->getOperand(Op+3);
231
232 if (BaseReg.isFrameIndex()) {
233 O << "[frame slot #" << BaseReg.getFrameIndex();
234 if (DispSpec.getImmedValue())
235 O << " + " << DispSpec.getImmedValue();
236 O << "]";
237 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000238 }
239
Evan Chenga09bd812006-02-26 08:28:12 +0000240 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000241 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000242 } else {
243 int DispVal = DispSpec.getImmedValue();
244 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
245 O << DispVal;
246 }
247
248 if (IndexReg.getReg() || BaseReg.getReg()) {
249 O << "(";
250 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000251 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000252
253 if (IndexReg.getReg()) {
254 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000255 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000256 if (ScaleVal != 1)
257 O << "," << ScaleVal;
258 }
259
260 O << ")";
261 }
262}
263
Evan Cheng7ccced62006-02-18 00:15:05 +0000264void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
265 O << "\"L" << getFunctionNumber() << "$pb\"\n";
266 O << "\"L" << getFunctionNumber() << "$pb\":";
267}
268
Evan Cheng62f27002006-04-28 23:11:40 +0000269
Evan Cheng55c25f22006-04-28 23:19:39 +0000270bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000271 const char Mode) {
272 const MRegisterInfo &RI = *TM.getRegisterInfo();
273 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000274 switch (Mode) {
275 default: return true; // Unknown mode.
276 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000277 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000278 break;
279 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000280 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000281 break;
282 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000283 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000284 break;
285 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000286 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000287 break;
288 }
289
Evan Cheng8f7f7122006-05-05 05:40:20 +0000290 O << '%';
291 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
292 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000293 return false;
294}
295
Evan Cheng3d48a902006-04-28 21:19:05 +0000296/// PrintAsmOperand - Print out an operand for an inline asm expression.
297///
298bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
299 unsigned AsmVariant,
300 const char *ExtraCode) {
301 // Does this asm operand have a single letter operand modifier?
302 if (ExtraCode && ExtraCode[0]) {
303 if (ExtraCode[1] != 0) return true; // Unknown modifier.
304
305 switch (ExtraCode[0]) {
306 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000307 case 'b': // Print QImode register
308 case 'h': // Print QImode high register
309 case 'w': // Print HImode register
310 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000311 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000312 }
313 }
314
315 printOperand(MI, OpNo);
316 return false;
317}
318
319bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
320 unsigned OpNo,
321 unsigned AsmVariant,
322 const char *ExtraCode) {
323 if (ExtraCode && ExtraCode[0])
324 return true; // Unknown modifier.
325 printMemReference(MI, OpNo);
326 return false;
327}
328
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000329/// printMachineInstruction -- Print out a single X86 LLVM instruction
330/// MI in Intel syntax to the current output stream.
331///
332void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
333 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000334 // This works around some Darwin assembler bugs.
335 if (forDarwin) {
336 switch (MI->getOpcode()) {
337 case X86::REP_MOVSB:
338 O << "rep/movsb (%esi),(%edi)\n";
339 return;
340 case X86::REP_MOVSD:
341 O << "rep/movsl (%esi),(%edi)\n";
342 return;
343 case X86::REP_MOVSW:
344 O << "rep/movsw (%esi),(%edi)\n";
345 return;
346 case X86::REP_STOSB:
347 O << "rep/stosb\n";
348 return;
349 case X86::REP_STOSD:
350 O << "rep/stosl\n";
351 return;
352 case X86::REP_STOSW:
353 O << "rep/stosw\n";
354 return;
355 default:
356 break;
357 }
358 }
359
Evan Cheng8f7f7122006-05-05 05:40:20 +0000360 // See if a truncate instruction can be turned into a nop.
361 switch (MI->getOpcode()) {
362 default: break;
Evan Cheng069287d2006-05-16 07:21:53 +0000363 case X86::TRUNC_GR32_GR16:
364 case X86::TRUNC_GR32_GR8:
365 case X86::TRUNC_GR16_GR8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000366 const MachineOperand &MO0 = MI->getOperand(0);
367 const MachineOperand &MO1 = MI->getOperand(1);
368 unsigned Reg0 = MO0.getReg();
369 unsigned Reg1 = MO1.getReg();
Evan Cheng069287d2006-05-16 07:21:53 +0000370 if (MI->getOpcode() == X86::TRUNC_GR32_GR16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000371 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000372 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000373 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
374 O << CommentString << " TRUNCATE ";
375 if (Reg0 != Reg1)
376 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000377 break;
378 }
379 }
380
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000381 // Call the autogenerated instruction printer routines.
382 printInstruction(MI);
383}
384
385// Include the auto-generated portion of the assembly writer.
386#include "X86GenAsmWriter.inc"
387