blob: 7baa8f87178244774e8f193d1d2af6d76d376e56 [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.
49 SwitchSection(".text", F);
50 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
51 break;
52 case Function::ExternalLinkage:
53 SwitchSection(".text", F);
54 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) {
60 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
61 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";
68 O << "\t.weak " << CurrentFnName << "\n";
69 }
70 break;
71 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000072 O << CurrentFnName << ":\n";
73
Jim Laskey6b92b8e2006-04-07 20:44:42 +000074 if (forDarwin) {
75 // Emit pre-function debug information.
76 DW.BeginFunction(&MF);
77 }
78
Chris Lattnerb36cbd02005-07-01 22:44:09 +000079 // Print out code for the function.
80 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
81 I != E; ++I) {
82 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +000083 if (I->pred_begin() != I->pred_end()) {
84 printBasicBlockLabel(I, true);
85 O << '\n';
86 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000087 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
88 II != E; ++II) {
89 // Print the assembly for the instruction.
90 O << "\t";
91 printMachineInstruction(II);
92 }
93 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000094 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000095 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000096
Evan Chengd5948812006-03-07 02:23:26 +000097 if (forDarwin) {
98 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +000099 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000100 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000101
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000102 // We didn't modify anything.
103 return false;
104}
105
Chris Lattnera3b8c572006-02-06 23:41:19 +0000106void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
107 const char *Modifier) {
108 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000109 const MRegisterInfo &RI = *TM.getRegisterInfo();
110 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000111 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000112 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
113 "Virtual registers should not make it this far!");
114 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000115 unsigned Reg = MO.getReg();
116 if (Modifier && strncmp(Modifier, "trunc", strlen("trunc")) == 0) {
117 MVT::ValueType VT = (strcmp(Modifier,"trunc16") == 0)
Evan Cheng403be7e2006-05-08 08:01:26 +0000118 ? MVT::i16 : MVT::i8;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000119 Reg = getX86SubSuperRegister(Reg, VT);
120 }
121 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000122 O << (char)tolower(*Name);
123 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000124 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000125
Chris Lattner63b3d712006-05-04 17:21:20 +0000126 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000127 if (!Modifier || strcmp(Modifier, "debug") != 0)
128 O << '$';
129 O << (int)MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000130 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000131 case MachineOperand::MO_MachineBasicBlock:
132 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000133 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000134 case MachineOperand::MO_JumpTableIndex: {
135 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
136 if (!isMemOp) O << '$';
137 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
138 << MO.getJumpTableIndex();
139 // FIXME: PIC relocation model
140 return;
141 }
Evan Chenga09bd812006-02-26 08:28:12 +0000142 case MachineOperand::MO_ConstantPoolIndex: {
143 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
144 if (!isMemOp) O << '$';
145 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
146 << MO.getConstantPoolIndex();
147 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
148 O << "-\"L" << getFunctionNumber() << "$pb\"";
149 int Offset = MO.getOffset();
150 if (Offset > 0)
151 O << "+" << Offset;
152 else if (Offset < 0)
153 O << Offset;
154 return;
155 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000156 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000157 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000158 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000159 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000160 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000161 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000162 GlobalValue *GV = MO.getGlobal();
163 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 // Link-once, External, or Weakly-linked global variables need
165 // non-lazily-resolved stubs
166 if (GV->isExternal() || GV->hasWeakLinkage() ||
167 GV->hasLinkOnceLinkage()) {
168 // Dynamically-resolved functions need a stub for the function.
169 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
170 FnStubs.insert(Name);
171 O << "L" << Name << "$stub";
172 } else {
173 GVStubs.insert(Name);
174 O << "L" << Name << "$non_lazy_ptr";
175 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000176 } else {
177 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000178 }
179 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
180 O << "-\"L" << getFunctionNumber() << "$pb\"";
181 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000182 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000183 int Offset = MO.getOffset();
184 if (Offset > 0)
185 O << "+" << Offset;
186 else if (Offset < 0)
187 O << Offset;
188 return;
189 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000190 case MachineOperand::MO_ExternalSymbol: {
191 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000192 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
193 std::string Name(GlobalPrefix);
194 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000195 FnStubs.insert(Name);
196 O << "L" << Name << "$stub";
197 return;
198 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000199 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000200 O << GlobalPrefix << MO.getSymbolName();
201 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000202 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000203 default:
204 O << "<unknown operand type>"; return;
205 }
206}
207
Nate Begeman391c5d22005-11-30 18:54:35 +0000208void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000209 unsigned char value = MI->getOperand(Op).getImmedValue();
210 assert(value <= 7 && "Invalid ssecc argument!");
211 switch (value) {
212 case 0: O << "eq"; break;
213 case 1: O << "lt"; break;
214 case 2: O << "le"; break;
215 case 3: O << "unord"; break;
216 case 4: O << "neq"; break;
217 case 5: O << "nlt"; break;
218 case 6: O << "nle"; break;
219 case 7: O << "ord"; break;
220 }
221}
222
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000223void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
224 assert(isMem(MI, Op) && "Invalid memory reference!");
225
226 const MachineOperand &BaseReg = MI->getOperand(Op);
227 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
228 const MachineOperand &IndexReg = MI->getOperand(Op+2);
229 const MachineOperand &DispSpec = MI->getOperand(Op+3);
230
231 if (BaseReg.isFrameIndex()) {
232 O << "[frame slot #" << BaseReg.getFrameIndex();
233 if (DispSpec.getImmedValue())
234 O << " + " << DispSpec.getImmedValue();
235 O << "]";
236 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000237 }
238
Evan Chenga09bd812006-02-26 08:28:12 +0000239 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000240 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000241 } else {
242 int DispVal = DispSpec.getImmedValue();
243 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
244 O << DispVal;
245 }
246
247 if (IndexReg.getReg() || BaseReg.getReg()) {
248 O << "(";
249 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000250 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000251
252 if (IndexReg.getReg()) {
253 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000254 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000255 if (ScaleVal != 1)
256 O << "," << ScaleVal;
257 }
258
259 O << ")";
260 }
261}
262
Evan Cheng7ccced62006-02-18 00:15:05 +0000263void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
264 O << "\"L" << getFunctionNumber() << "$pb\"\n";
265 O << "\"L" << getFunctionNumber() << "$pb\":";
266}
267
Evan Cheng62f27002006-04-28 23:11:40 +0000268
Evan Cheng55c25f22006-04-28 23:19:39 +0000269bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000270 const char Mode) {
271 const MRegisterInfo &RI = *TM.getRegisterInfo();
272 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000273 switch (Mode) {
274 default: return true; // Unknown mode.
275 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000276 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000277 break;
278 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000279 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000280 break;
281 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000282 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000283 break;
284 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000285 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000286 break;
287 }
288
Evan Cheng8f7f7122006-05-05 05:40:20 +0000289 O << '%';
290 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
291 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000292 return false;
293}
294
Evan Cheng3d48a902006-04-28 21:19:05 +0000295/// PrintAsmOperand - Print out an operand for an inline asm expression.
296///
297bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
298 unsigned AsmVariant,
299 const char *ExtraCode) {
300 // Does this asm operand have a single letter operand modifier?
301 if (ExtraCode && ExtraCode[0]) {
302 if (ExtraCode[1] != 0) return true; // Unknown modifier.
303
304 switch (ExtraCode[0]) {
305 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000306 case 'b': // Print QImode register
307 case 'h': // Print QImode high register
308 case 'w': // Print HImode register
309 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000310 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000311 }
312 }
313
314 printOperand(MI, OpNo);
315 return false;
316}
317
318bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
319 unsigned OpNo,
320 unsigned AsmVariant,
321 const char *ExtraCode) {
322 if (ExtraCode && ExtraCode[0])
323 return true; // Unknown modifier.
324 printMemReference(MI, OpNo);
325 return false;
326}
327
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000328/// printMachineInstruction -- Print out a single X86 LLVM instruction
329/// MI in Intel syntax to the current output stream.
330///
331void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
332 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000333 // This works around some Darwin assembler bugs.
334 if (forDarwin) {
335 switch (MI->getOpcode()) {
336 case X86::REP_MOVSB:
337 O << "rep/movsb (%esi),(%edi)\n";
338 return;
339 case X86::REP_MOVSD:
340 O << "rep/movsl (%esi),(%edi)\n";
341 return;
342 case X86::REP_MOVSW:
343 O << "rep/movsw (%esi),(%edi)\n";
344 return;
345 case X86::REP_STOSB:
346 O << "rep/stosb\n";
347 return;
348 case X86::REP_STOSD:
349 O << "rep/stosl\n";
350 return;
351 case X86::REP_STOSW:
352 O << "rep/stosw\n";
353 return;
354 default:
355 break;
356 }
357 }
358
Evan Cheng8f7f7122006-05-05 05:40:20 +0000359 // See if a truncate instruction can be turned into a nop.
360 switch (MI->getOpcode()) {
361 default: break;
362 case X86::TRUNC_R32_R16:
363 case X86::TRUNC_R32_R8:
364 case X86::TRUNC_R16_R8: {
365 const MachineOperand &MO0 = MI->getOperand(0);
366 const MachineOperand &MO1 = MI->getOperand(1);
367 unsigned Reg0 = MO0.getReg();
368 unsigned Reg1 = MO1.getReg();
Evan Cheng403be7e2006-05-08 08:01:26 +0000369 if (MI->getOpcode() == X86::TRUNC_R32_R16)
370 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000371 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000372 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
373 O << CommentString << " TRUNCATE ";
374 if (Reg0 != Reg1)
375 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000376 break;
377 }
378 }
379
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000380 // Call the autogenerated instruction printer routines.
381 printInstruction(MI);
382}
383
384// Include the auto-generated portion of the assembly writer.
385#include "X86GenAsmWriter.inc"
386