blob: 3e690aa398d8ff1a7c5d5a1720827b8e2311ecd9 [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>());
Evan Cheng3c992d22006-03-07 02:02:57 +000032
Chris Lattner8b8b9512005-11-21 07:51:23 +000033 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034 O << "\n\n";
35
36 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000037 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000038
Nate Begeman37efe672006-04-22 18:53:45 +000039 // Print out jump tables referenced by the function
40 EmitJumpTableInfo(MF.getJumpTableInfo());
41
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000043 const Function *F = MF.getFunction();
44 switch (F->getLinkage()) {
45 default: assert(0 && "Unknown linkage type!");
46 case Function::InternalLinkage: // Symbols default to internal.
Chris Lattnerdad9c5a2006-05-09 05:12:53 +000047 SwitchToTextSection(DefaultTextSection, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000048 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
49 break;
50 case Function::ExternalLinkage:
Chris Lattnerdad9c5a2006-05-09 05:12:53 +000051 SwitchToTextSection(DefaultTextSection, F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000052 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Chris Lattner272f9982005-12-16 00:07:30 +000053 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000054 break;
55 case Function::WeakLinkage:
56 case Function::LinkOnceLinkage:
Evan Cheng932ad512006-05-25 21:59:08 +000057 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Chris Lattner4632d7a2006-05-09 04:59:56 +000058 SwitchToTextSection(
59 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
Evan Chengf1616da2006-02-22 23:59:57 +000060 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000061 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Evan Cheng932ad512006-05-25 21:59:08 +000062 } else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
63 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
64 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
65 << ",\"ax\"\n";
66 SwitchToTextSection("", F);
67 O << "\t.weak " << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000068 } else {
69 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
70 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
71 << ",\"ax\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +000072 SwitchToTextSection("", F);
Evan Cheng2338c5c2006-02-07 08:38:37 +000073 O << "\t.weak " << CurrentFnName << "\n";
74 }
75 break;
76 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000077 O << CurrentFnName << ":\n";
78
Evan Cheng932ad512006-05-25 21:59:08 +000079 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +000080 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +000081 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +000082 }
83
Chris Lattnerb36cbd02005-07-01 22:44:09 +000084 // Print out code for the function.
85 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
86 I != E; ++I) {
87 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +000088 if (I->pred_begin() != I->pred_end()) {
89 printBasicBlockLabel(I, true);
90 O << '\n';
91 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000092 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
93 II != E; ++II) {
94 // Print the assembly for the instruction.
95 O << "\t";
96 printMachineInstruction(II);
97 }
98 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000099 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +0000100 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000101
Evan Cheng932ad512006-05-25 21:59:08 +0000102 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Evan Chengd5948812006-03-07 02:23:26 +0000103 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000104 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000105 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000106
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000107 // We didn't modify anything.
108 return false;
109}
110
Chris Lattnera3b8c572006-02-06 23:41:19 +0000111void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
112 const char *Modifier) {
113 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000114 const MRegisterInfo &RI = *TM.getRegisterInfo();
115 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000116 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000117 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
118 "Virtual registers should not make it this far!");
119 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000120 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000121 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
122 MVT::ValueType VT = (strcmp(Modifier,"subreg16") == 0)
Evan Cheng403be7e2006-05-08 08:01:26 +0000123 ? MVT::i16 : MVT::i8;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000124 Reg = getX86SubSuperRegister(Reg, VT);
125 }
126 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000127 O << (char)tolower(*Name);
128 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000129 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000130
Chris Lattner63b3d712006-05-04 17:21:20 +0000131 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000132 if (!Modifier || strcmp(Modifier, "debug") != 0)
133 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000134 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000135 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000136 case MachineOperand::MO_MachineBasicBlock:
137 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000138 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000139 case MachineOperand::MO_JumpTableIndex: {
140 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
141 if (!isMemOp) O << '$';
142 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
143 << MO.getJumpTableIndex();
144 // FIXME: PIC relocation model
145 return;
146 }
Evan Chenga09bd812006-02-26 08:28:12 +0000147 case MachineOperand::MO_ConstantPoolIndex: {
148 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
149 if (!isMemOp) O << '$';
150 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
151 << MO.getConstantPoolIndex();
Evan Cheng932ad512006-05-25 21:59:08 +0000152 if (Subtarget->TargetType == X86Subtarget::isDarwin &&
153 TM.getRelocationModel() == Reloc::PIC)
Evan Chenga09bd812006-02-26 08:28:12 +0000154 O << "-\"L" << getFunctionNumber() << "$pb\"";
155 int Offset = MO.getOffset();
156 if (Offset > 0)
157 O << "+" << Offset;
158 else if (Offset < 0)
159 O << Offset;
160 return;
161 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000162 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000163 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000164 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000165 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000166 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng932ad512006-05-25 21:59:08 +0000167 if (Subtarget->TargetType == X86Subtarget::isDarwin &&
168 TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000169 GlobalValue *GV = MO.getGlobal();
170 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000171 // Link-once, External, or Weakly-linked global variables need
172 // non-lazily-resolved stubs
173 if (GV->isExternal() || GV->hasWeakLinkage() ||
174 GV->hasLinkOnceLinkage()) {
175 // Dynamically-resolved functions need a stub for the function.
176 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
177 FnStubs.insert(Name);
178 O << "L" << Name << "$stub";
179 } else {
180 GVStubs.insert(Name);
181 O << "L" << Name << "$non_lazy_ptr";
182 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000183 } else {
184 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000185 }
186 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
187 O << "-\"L" << getFunctionNumber() << "$pb\"";
188 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000189 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000190 int Offset = MO.getOffset();
191 if (Offset > 0)
192 O << "+" << Offset;
193 else if (Offset < 0)
194 O << Offset;
195 return;
196 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000197 case MachineOperand::MO_ExternalSymbol: {
198 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000199 if (isCallOp &&
200 Subtarget->TargetType == X86Subtarget::isDarwin &&
201 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng4c1aa862006-02-22 20:19:42 +0000202 std::string Name(GlobalPrefix);
203 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000204 FnStubs.insert(Name);
205 O << "L" << Name << "$stub";
206 return;
207 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000208 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209 O << GlobalPrefix << MO.getSymbolName();
210 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000211 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000212 default:
213 O << "<unknown operand type>"; return;
214 }
215}
216
Nate Begeman391c5d22005-11-30 18:54:35 +0000217void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000218 unsigned char value = MI->getOperand(Op).getImmedValue();
219 assert(value <= 7 && "Invalid ssecc argument!");
220 switch (value) {
221 case 0: O << "eq"; break;
222 case 1: O << "lt"; break;
223 case 2: O << "le"; break;
224 case 3: O << "unord"; break;
225 case 4: O << "neq"; break;
226 case 5: O << "nlt"; break;
227 case 6: O << "nle"; break;
228 case 7: O << "ord"; break;
229 }
230}
231
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000232void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
233 assert(isMem(MI, Op) && "Invalid memory reference!");
234
235 const MachineOperand &BaseReg = MI->getOperand(Op);
236 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
237 const MachineOperand &IndexReg = MI->getOperand(Op+2);
238 const MachineOperand &DispSpec = MI->getOperand(Op+3);
239
240 if (BaseReg.isFrameIndex()) {
241 O << "[frame slot #" << BaseReg.getFrameIndex();
242 if (DispSpec.getImmedValue())
243 O << " + " << DispSpec.getImmedValue();
244 O << "]";
245 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000246 }
247
Evan Chenga09bd812006-02-26 08:28:12 +0000248 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000249 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000250 } else {
251 int DispVal = DispSpec.getImmedValue();
252 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
253 O << DispVal;
254 }
255
256 if (IndexReg.getReg() || BaseReg.getReg()) {
257 O << "(";
258 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000259 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000260
261 if (IndexReg.getReg()) {
262 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000263 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000264 if (ScaleVal != 1)
265 O << "," << ScaleVal;
266 }
267
268 O << ")";
269 }
270}
271
Evan Cheng7ccced62006-02-18 00:15:05 +0000272void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
273 O << "\"L" << getFunctionNumber() << "$pb\"\n";
274 O << "\"L" << getFunctionNumber() << "$pb\":";
275}
276
Evan Cheng62f27002006-04-28 23:11:40 +0000277
Evan Cheng55c25f22006-04-28 23:19:39 +0000278bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000279 const char Mode) {
280 const MRegisterInfo &RI = *TM.getRegisterInfo();
281 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000282 switch (Mode) {
283 default: return true; // Unknown mode.
284 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000285 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000286 break;
287 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000288 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000289 break;
290 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000291 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000292 break;
293 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000294 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000295 break;
296 }
297
Evan Cheng8f7f7122006-05-05 05:40:20 +0000298 O << '%';
299 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
300 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000301 return false;
302}
303
Evan Cheng3d48a902006-04-28 21:19:05 +0000304/// PrintAsmOperand - Print out an operand for an inline asm expression.
305///
306bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
307 unsigned AsmVariant,
308 const char *ExtraCode) {
309 // Does this asm operand have a single letter operand modifier?
310 if (ExtraCode && ExtraCode[0]) {
311 if (ExtraCode[1] != 0) return true; // Unknown modifier.
312
313 switch (ExtraCode[0]) {
314 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000315 case 'b': // Print QImode register
316 case 'h': // Print QImode high register
317 case 'w': // Print HImode register
318 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000319 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000320 }
321 }
322
323 printOperand(MI, OpNo);
324 return false;
325}
326
327bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
328 unsigned OpNo,
329 unsigned AsmVariant,
330 const char *ExtraCode) {
331 if (ExtraCode && ExtraCode[0])
332 return true; // Unknown modifier.
333 printMemReference(MI, OpNo);
334 return false;
335}
336
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000337/// printMachineInstruction -- Print out a single X86 LLVM instruction
338/// MI in Intel syntax to the current output stream.
339///
340void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
341 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000342 // This works around some Darwin assembler bugs.
Evan Cheng932ad512006-05-25 21:59:08 +0000343 if (Subtarget->TargetType == X86Subtarget::isDarwin) {
Evan Cheng67caa392006-01-26 02:27:43 +0000344 switch (MI->getOpcode()) {
345 case X86::REP_MOVSB:
346 O << "rep/movsb (%esi),(%edi)\n";
347 return;
348 case X86::REP_MOVSD:
349 O << "rep/movsl (%esi),(%edi)\n";
350 return;
351 case X86::REP_MOVSW:
352 O << "rep/movsw (%esi),(%edi)\n";
353 return;
354 case X86::REP_STOSB:
355 O << "rep/stosb\n";
356 return;
357 case X86::REP_STOSD:
358 O << "rep/stosl\n";
359 return;
360 case X86::REP_STOSW:
361 O << "rep/stosw\n";
362 return;
363 default:
364 break;
365 }
366 }
367
Evan Cheng8f7f7122006-05-05 05:40:20 +0000368 // See if a truncate instruction can be turned into a nop.
369 switch (MI->getOpcode()) {
370 default: break;
Evan Cheng069287d2006-05-16 07:21:53 +0000371 case X86::TRUNC_GR32_GR16:
372 case X86::TRUNC_GR32_GR8:
373 case X86::TRUNC_GR16_GR8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000374 const MachineOperand &MO0 = MI->getOperand(0);
375 const MachineOperand &MO1 = MI->getOperand(1);
376 unsigned Reg0 = MO0.getReg();
377 unsigned Reg1 = MO1.getReg();
Evan Cheng069287d2006-05-16 07:21:53 +0000378 if (MI->getOpcode() == X86::TRUNC_GR32_GR16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000379 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000380 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000381 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
382 O << CommentString << " TRUNCATE ";
383 if (Reg0 != Reg1)
384 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000385 break;
386 }
387 }
388
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000389 // Call the autogenerated instruction printer routines.
390 printInstruction(MI);
391}
392
393// Include the auto-generated portion of the assembly writer.
394#include "X86GenAsmWriter.inc"
395