blob: 5a3490a703457b423f8b599785ec95407244f87f [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
41 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000042 const Function *F = MF.getFunction();
43 switch (F->getLinkage()) {
44 default: assert(0 && "Unknown linkage type!");
45 case Function::InternalLinkage: // Symbols default to internal.
46 SwitchSection(".text", F);
47 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
48 break;
49 case Function::ExternalLinkage:
50 SwitchSection(".text", F);
51 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Chris Lattner272f9982005-12-16 00:07:30 +000052 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000053 break;
54 case Function::WeakLinkage:
55 case Function::LinkOnceLinkage:
56 if (forDarwin) {
57 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
58 F);
Evan Chengf1616da2006-02-22 23:59:57 +000059 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000060 O << "\t.weak_definition\t" << CurrentFnName << "\n";
61 } else {
62 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
63 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
64 << ",\"ax\",@progbits\n";
65 O << "\t.weak " << CurrentFnName << "\n";
66 }
67 break;
68 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000069 O << CurrentFnName << ":\n";
70
Jim Laskey6b92b8e2006-04-07 20:44:42 +000071 if (forDarwin) {
72 // Emit pre-function debug information.
73 DW.BeginFunction(&MF);
74 }
75
Chris Lattnerb36cbd02005-07-01 22:44:09 +000076 // Print out code for the function.
77 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
78 I != E; ++I) {
79 // Print a label for the basic block.
80 if (I->pred_begin() != I->pred_end())
Chris Lattner64965ba2005-11-21 07:43:59 +000081 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
82 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
83 << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000084 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
85 II != E; ++II) {
86 // Print the assembly for the instruction.
87 O << "\t";
88 printMachineInstruction(II);
89 }
90 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000091 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000092 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000093
Evan Chengd5948812006-03-07 02:23:26 +000094 if (forDarwin) {
95 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +000096 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +000097 }
Evan Cheng3c992d22006-03-07 02:02:57 +000098
Chris Lattnerb36cbd02005-07-01 22:44:09 +000099 // We didn't modify anything.
100 return false;
101}
102
Chris Lattnera3b8c572006-02-06 23:41:19 +0000103void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
104 const char *Modifier) {
105 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000106 const MRegisterInfo &RI = *TM.getRegisterInfo();
107 switch (MO.getType()) {
108 case MachineOperand::MO_VirtualRegister:
109 case MachineOperand::MO_MachineRegister:
110 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
111 "Virtual registers should not make it this far!");
112 O << '%';
113 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
114 O << (char)tolower(*Name);
115 return;
116
117 case MachineOperand::MO_SignExtendedImmed:
118 case MachineOperand::MO_UnextendedImmed:
Evan Cheng3c992d22006-03-07 02:02:57 +0000119 if (!Modifier || strcmp(Modifier, "debug") != 0)
120 O << '$';
121 O << (int)MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000122 return;
123 case MachineOperand::MO_MachineBasicBlock: {
124 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner64965ba2005-11-21 07:43:59 +0000125 O << PrivateGlobalPrefix << "BB"
126 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000127 << "_" << MBBOp->getNumber () << "\t# "
128 << MBBOp->getBasicBlock ()->getName ();
129 return;
130 }
131 case MachineOperand::MO_PCRelativeDisp:
132 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
133 abort ();
134 return;
Evan Chenga09bd812006-02-26 08:28:12 +0000135 case MachineOperand::MO_ConstantPoolIndex: {
136 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
137 if (!isMemOp) O << '$';
138 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
139 << MO.getConstantPoolIndex();
140 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
141 O << "-\"L" << getFunctionNumber() << "$pb\"";
142 int Offset = MO.getOffset();
143 if (Offset > 0)
144 O << "+" << Offset;
145 else if (Offset < 0)
146 O << Offset;
147 return;
148 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000149 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000150 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000151 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000152 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000153 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000154 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000155 GlobalValue *GV = MO.getGlobal();
156 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000157 // Link-once, External, or Weakly-linked global variables need
158 // non-lazily-resolved stubs
159 if (GV->isExternal() || GV->hasWeakLinkage() ||
160 GV->hasLinkOnceLinkage()) {
161 // Dynamically-resolved functions need a stub for the function.
162 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
163 FnStubs.insert(Name);
164 O << "L" << Name << "$stub";
165 } else {
166 GVStubs.insert(Name);
167 O << "L" << Name << "$non_lazy_ptr";
168 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000169 } else {
170 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000171 }
172 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
173 O << "-\"L" << getFunctionNumber() << "$pb\"";
174 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000175 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000176 int Offset = MO.getOffset();
177 if (Offset > 0)
178 O << "+" << Offset;
179 else if (Offset < 0)
180 O << Offset;
181 return;
182 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000183 case MachineOperand::MO_ExternalSymbol: {
184 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000185 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
186 std::string Name(GlobalPrefix);
187 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000188 FnStubs.insert(Name);
189 O << "L" << Name << "$stub";
190 return;
191 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000192 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000193 O << GlobalPrefix << MO.getSymbolName();
194 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000195 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000196 default:
197 O << "<unknown operand type>"; return;
198 }
199}
200
Nate Begeman391c5d22005-11-30 18:54:35 +0000201void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000202 unsigned char value = MI->getOperand(Op).getImmedValue();
203 assert(value <= 7 && "Invalid ssecc argument!");
204 switch (value) {
205 case 0: O << "eq"; break;
206 case 1: O << "lt"; break;
207 case 2: O << "le"; break;
208 case 3: O << "unord"; break;
209 case 4: O << "neq"; break;
210 case 5: O << "nlt"; break;
211 case 6: O << "nle"; break;
212 case 7: O << "ord"; break;
213 }
214}
215
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000216void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
217 assert(isMem(MI, Op) && "Invalid memory reference!");
218
219 const MachineOperand &BaseReg = MI->getOperand(Op);
220 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
221 const MachineOperand &IndexReg = MI->getOperand(Op+2);
222 const MachineOperand &DispSpec = MI->getOperand(Op+3);
223
224 if (BaseReg.isFrameIndex()) {
225 O << "[frame slot #" << BaseReg.getFrameIndex();
226 if (DispSpec.getImmedValue())
227 O << " + " << DispSpec.getImmedValue();
228 O << "]";
229 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000230 }
231
Evan Chenga09bd812006-02-26 08:28:12 +0000232 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000233 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000234 } else {
235 int DispVal = DispSpec.getImmedValue();
236 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
237 O << DispVal;
238 }
239
240 if (IndexReg.getReg() || BaseReg.getReg()) {
241 O << "(";
242 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000243 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000244
245 if (IndexReg.getReg()) {
246 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000247 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000248 if (ScaleVal != 1)
249 O << "," << ScaleVal;
250 }
251
252 O << ")";
253 }
254}
255
Evan Cheng7ccced62006-02-18 00:15:05 +0000256void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
257 O << "\"L" << getFunctionNumber() << "$pb\"\n";
258 O << "\"L" << getFunctionNumber() << "$pb\":";
259}
260
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000261/// printMachineInstruction -- Print out a single X86 LLVM instruction
262/// MI in Intel syntax to the current output stream.
263///
264void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
265 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000266 // This works around some Darwin assembler bugs.
267 if (forDarwin) {
268 switch (MI->getOpcode()) {
269 case X86::REP_MOVSB:
270 O << "rep/movsb (%esi),(%edi)\n";
271 return;
272 case X86::REP_MOVSD:
273 O << "rep/movsl (%esi),(%edi)\n";
274 return;
275 case X86::REP_MOVSW:
276 O << "rep/movsw (%esi),(%edi)\n";
277 return;
278 case X86::REP_STOSB:
279 O << "rep/stosb\n";
280 return;
281 case X86::REP_STOSD:
282 O << "rep/stosl\n";
283 return;
284 case X86::REP_STOSW:
285 O << "rep/stosw\n";
286 return;
287 default:
288 break;
289 }
290 }
291
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000292 // Call the autogenerated instruction printer routines.
293 printInstruction(MI);
294}
295
296// Include the auto-generated portion of the assembly writer.
297#include "X86GenAsmWriter.inc"
298