blob: 3afa1f7a8edcf369111ae9dde207340c03df5cd4 [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.
83 if (I->pred_begin() != I->pred_end())
Chris Lattner64965ba2005-11-21 07:43:59 +000084 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
85 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
86 << "\n";
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()) {
111 case MachineOperand::MO_VirtualRegister:
112 case MachineOperand::MO_MachineRegister:
113 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
114 "Virtual registers should not make it this far!");
115 O << '%';
116 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
117 O << (char)tolower(*Name);
118 return;
119
120 case MachineOperand::MO_SignExtendedImmed:
121 case MachineOperand::MO_UnextendedImmed:
Evan Cheng3c992d22006-03-07 02:02:57 +0000122 if (!Modifier || strcmp(Modifier, "debug") != 0)
123 O << '$';
124 O << (int)MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000125 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000126 case MachineOperand::MO_MachineBasicBlock:
127 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000128 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000129 case MachineOperand::MO_PCRelativeDisp:
130 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
131 abort ();
132 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000133 case MachineOperand::MO_JumpTableIndex: {
134 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
135 if (!isMemOp) O << '$';
136 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
137 << MO.getJumpTableIndex();
138 // FIXME: PIC relocation model
139 return;
140 }
Evan Chenga09bd812006-02-26 08:28:12 +0000141 case MachineOperand::MO_ConstantPoolIndex: {
142 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
143 if (!isMemOp) O << '$';
144 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
145 << MO.getConstantPoolIndex();
146 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
147 O << "-\"L" << getFunctionNumber() << "$pb\"";
148 int Offset = MO.getOffset();
149 if (Offset > 0)
150 O << "+" << Offset;
151 else if (Offset < 0)
152 O << Offset;
153 return;
154 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000155 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000156 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000157 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000158 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000159 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000160 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000161 GlobalValue *GV = MO.getGlobal();
162 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000163 // Link-once, External, or Weakly-linked global variables need
164 // non-lazily-resolved stubs
165 if (GV->isExternal() || GV->hasWeakLinkage() ||
166 GV->hasLinkOnceLinkage()) {
167 // Dynamically-resolved functions need a stub for the function.
168 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
169 FnStubs.insert(Name);
170 O << "L" << Name << "$stub";
171 } else {
172 GVStubs.insert(Name);
173 O << "L" << Name << "$non_lazy_ptr";
174 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000175 } else {
176 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000177 }
178 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
179 O << "-\"L" << getFunctionNumber() << "$pb\"";
180 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000181 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000182 int Offset = MO.getOffset();
183 if (Offset > 0)
184 O << "+" << Offset;
185 else if (Offset < 0)
186 O << Offset;
187 return;
188 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000189 case MachineOperand::MO_ExternalSymbol: {
190 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000191 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
192 std::string Name(GlobalPrefix);
193 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000194 FnStubs.insert(Name);
195 O << "L" << Name << "$stub";
196 return;
197 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000198 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000199 O << GlobalPrefix << MO.getSymbolName();
200 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000201 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000202 default:
203 O << "<unknown operand type>"; return;
204 }
205}
206
Nate Begeman391c5d22005-11-30 18:54:35 +0000207void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000208 unsigned char value = MI->getOperand(Op).getImmedValue();
209 assert(value <= 7 && "Invalid ssecc argument!");
210 switch (value) {
211 case 0: O << "eq"; break;
212 case 1: O << "lt"; break;
213 case 2: O << "le"; break;
214 case 3: O << "unord"; break;
215 case 4: O << "neq"; break;
216 case 5: O << "nlt"; break;
217 case 6: O << "nle"; break;
218 case 7: O << "ord"; break;
219 }
220}
221
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000222void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
223 assert(isMem(MI, Op) && "Invalid memory reference!");
224
225 const MachineOperand &BaseReg = MI->getOperand(Op);
226 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
227 const MachineOperand &IndexReg = MI->getOperand(Op+2);
228 const MachineOperand &DispSpec = MI->getOperand(Op+3);
229
230 if (BaseReg.isFrameIndex()) {
231 O << "[frame slot #" << BaseReg.getFrameIndex();
232 if (DispSpec.getImmedValue())
233 O << " + " << DispSpec.getImmedValue();
234 O << "]";
235 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000236 }
237
Evan Chenga09bd812006-02-26 08:28:12 +0000238 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000239 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000240 } else {
241 int DispVal = DispSpec.getImmedValue();
242 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
243 O << DispVal;
244 }
245
246 if (IndexReg.getReg() || BaseReg.getReg()) {
247 O << "(";
248 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000249 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000250
251 if (IndexReg.getReg()) {
252 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000253 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000254 if (ScaleVal != 1)
255 O << "," << ScaleVal;
256 }
257
258 O << ")";
259 }
260}
261
Evan Cheng7ccced62006-02-18 00:15:05 +0000262void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
263 O << "\"L" << getFunctionNumber() << "$pb\"\n";
264 O << "\"L" << getFunctionNumber() << "$pb\":";
265}
266
Evan Cheng3d48a902006-04-28 21:19:05 +0000267/// PrintAsmOperand - Print out an operand for an inline asm expression.
268///
269bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
270 unsigned AsmVariant,
271 const char *ExtraCode) {
272 // Does this asm operand have a single letter operand modifier?
273 if (ExtraCode && ExtraCode[0]) {
274 if (ExtraCode[1] != 0) return true; // Unknown modifier.
275
276 switch (ExtraCode[0]) {
277 default: return true; // Unknown modifier.
278 }
279 }
280
281 printOperand(MI, OpNo);
282 return false;
283}
284
285bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
286 unsigned OpNo,
287 unsigned AsmVariant,
288 const char *ExtraCode) {
289 if (ExtraCode && ExtraCode[0])
290 return true; // Unknown modifier.
291 printMemReference(MI, OpNo);
292 return false;
293}
294
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000295/// printMachineInstruction -- Print out a single X86 LLVM instruction
296/// MI in Intel syntax to the current output stream.
297///
298void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
299 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000300 // This works around some Darwin assembler bugs.
301 if (forDarwin) {
302 switch (MI->getOpcode()) {
303 case X86::REP_MOVSB:
304 O << "rep/movsb (%esi),(%edi)\n";
305 return;
306 case X86::REP_MOVSD:
307 O << "rep/movsl (%esi),(%edi)\n";
308 return;
309 case X86::REP_MOVSW:
310 O << "rep/movsw (%esi),(%edi)\n";
311 return;
312 case X86::REP_STOSB:
313 O << "rep/stosb\n";
314 return;
315 case X86::REP_STOSD:
316 O << "rep/stosl\n";
317 return;
318 case X86::REP_STOSW:
319 O << "rep/stosw\n";
320 return;
321 default:
322 break;
323 }
324 }
325
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000326 // Call the autogenerated instruction printer routines.
327 printInstruction(MI);
328}
329
330// Include the auto-generated portion of the assembly writer.
331#include "X86GenAsmWriter.inc"
332