blob: f1774fd8946c9f0cc9f61a7a43705c6785ec2fc2 [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;
24using namespace x86;
25
26/// runOnMachineFunction - This uses the printMachineInstruction()
27/// method to print assembly for each instruction.
28///
29bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +000030 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000031 O << "\n\n";
32
33 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000034 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035
36 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000037 const Function *F = MF.getFunction();
38 switch (F->getLinkage()) {
39 default: assert(0 && "Unknown linkage type!");
40 case Function::InternalLinkage: // Symbols default to internal.
41 SwitchSection(".text", F);
42 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
43 break;
44 case Function::ExternalLinkage:
45 SwitchSection(".text", F);
46 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Chris Lattner272f9982005-12-16 00:07:30 +000047 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000048 break;
49 case Function::WeakLinkage:
50 case Function::LinkOnceLinkage:
51 if (forDarwin) {
52 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
53 F);
54 O << "\t.weak_definition\t" << CurrentFnName << "\n";
55 } else {
56 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
57 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
58 << ",\"ax\",@progbits\n";
59 O << "\t.weak " << CurrentFnName << "\n";
60 }
61 break;
62 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000063 O << CurrentFnName << ":\n";
64
65 // Print out code for the function.
66 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
67 I != E; ++I) {
68 // Print a label for the basic block.
69 if (I->pred_begin() != I->pred_end())
Chris Lattner64965ba2005-11-21 07:43:59 +000070 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
71 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
72 << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000073 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
74 II != E; ++II) {
75 // Print the assembly for the instruction.
76 O << "\t";
77 printMachineInstruction(II);
78 }
79 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000080 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000081 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000082
83 // We didn't modify anything.
84 return false;
85}
86
Chris Lattnera3b8c572006-02-06 23:41:19 +000087void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
88 const char *Modifier) {
89 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000090 const MRegisterInfo &RI = *TM.getRegisterInfo();
91 switch (MO.getType()) {
92 case MachineOperand::MO_VirtualRegister:
93 case MachineOperand::MO_MachineRegister:
94 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
95 "Virtual registers should not make it this far!");
96 O << '%';
97 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
98 O << (char)tolower(*Name);
99 return;
100
101 case MachineOperand::MO_SignExtendedImmed:
102 case MachineOperand::MO_UnextendedImmed:
103 O << '$' << (int)MO.getImmedValue();
104 return;
105 case MachineOperand::MO_MachineBasicBlock: {
106 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner64965ba2005-11-21 07:43:59 +0000107 O << PrivateGlobalPrefix << "BB"
108 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000109 << "_" << MBBOp->getNumber () << "\t# "
110 << MBBOp->getBasicBlock ()->getName ();
111 return;
112 }
113 case MachineOperand::MO_PCRelativeDisp:
114 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
115 abort ();
116 return;
117 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000118 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000119 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000120 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000121 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000122 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000123 GlobalValue *GV = MO.getGlobal();
124 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000125 // Link-once, External, or Weakly-linked global variables need
126 // non-lazily-resolved stubs
127 if (GV->isExternal() || GV->hasWeakLinkage() ||
128 GV->hasLinkOnceLinkage()) {
129 // Dynamically-resolved functions need a stub for the function.
130 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
131 FnStubs.insert(Name);
132 O << "L" << Name << "$stub";
133 } else {
134 GVStubs.insert(Name);
135 O << "L" << Name << "$non_lazy_ptr";
Evan Cheng4c1aa862006-02-22 20:19:42 +0000136 if (TM.getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +0000137 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000138 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000139 } else {
140 O << Mang->getValueName(GV);
Nate Begeman72b286b2005-07-08 00:23:26 +0000141 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000142 } else
143 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000144 int Offset = MO.getOffset();
145 if (Offset > 0)
146 O << "+" << Offset;
147 else if (Offset < 0)
148 O << Offset;
149 return;
150 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000151 case MachineOperand::MO_ExternalSymbol: {
152 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000153 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
154 std::string Name(GlobalPrefix);
155 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000156 FnStubs.insert(Name);
157 O << "L" << Name << "$stub";
158 return;
159 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000160 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000161 O << GlobalPrefix << MO.getSymbolName();
162 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000163 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000164 default:
165 O << "<unknown operand type>"; return;
166 }
167}
168
Nate Begeman391c5d22005-11-30 18:54:35 +0000169void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000170 unsigned char value = MI->getOperand(Op).getImmedValue();
171 assert(value <= 7 && "Invalid ssecc argument!");
172 switch (value) {
173 case 0: O << "eq"; break;
174 case 1: O << "lt"; break;
175 case 2: O << "le"; break;
176 case 3: O << "unord"; break;
177 case 4: O << "neq"; break;
178 case 5: O << "nlt"; break;
179 case 6: O << "nle"; break;
180 case 7: O << "ord"; break;
181 }
182}
183
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000184void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
185 assert(isMem(MI, Op) && "Invalid memory reference!");
186
187 const MachineOperand &BaseReg = MI->getOperand(Op);
188 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
189 const MachineOperand &IndexReg = MI->getOperand(Op+2);
190 const MachineOperand &DispSpec = MI->getOperand(Op+3);
191
192 if (BaseReg.isFrameIndex()) {
193 O << "[frame slot #" << BaseReg.getFrameIndex();
194 if (DispSpec.getImmedValue())
195 O << " + " << DispSpec.getImmedValue();
196 O << "]";
197 return;
198 } else if (BaseReg.isConstantPoolIndex()) {
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000199 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000200 << BaseReg.getConstantPoolIndex();
Evan Cheng4c1aa862006-02-22 20:19:42 +0000201 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +0000202 O << "-\"L" << getFunctionNumber() << "$pb\"";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000203 if (DispSpec.getImmedValue())
204 O << "+" << DispSpec.getImmedValue();
205 if (IndexReg.getReg()) {
206 O << "(,";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000207 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000208 if (ScaleVal != 1)
209 O << "," << ScaleVal;
210 O << ")";
211 }
212 return;
213 }
214
215 if (DispSpec.isGlobalAddress()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000216 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000217 } else {
218 int DispVal = DispSpec.getImmedValue();
219 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
220 O << DispVal;
221 }
222
223 if (IndexReg.getReg() || BaseReg.getReg()) {
224 O << "(";
225 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000226 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000227
228 if (IndexReg.getReg()) {
229 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000230 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000231 if (ScaleVal != 1)
232 O << "," << ScaleVal;
233 }
234
235 O << ")";
236 }
237}
238
Evan Cheng7ccced62006-02-18 00:15:05 +0000239void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
240 O << "\"L" << getFunctionNumber() << "$pb\"\n";
241 O << "\"L" << getFunctionNumber() << "$pb\":";
242}
243
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000244/// printMachineInstruction -- Print out a single X86 LLVM instruction
245/// MI in Intel syntax to the current output stream.
246///
247void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
248 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000249 // This works around some Darwin assembler bugs.
250 if (forDarwin) {
251 switch (MI->getOpcode()) {
252 case X86::REP_MOVSB:
253 O << "rep/movsb (%esi),(%edi)\n";
254 return;
255 case X86::REP_MOVSD:
256 O << "rep/movsl (%esi),(%edi)\n";
257 return;
258 case X86::REP_MOVSW:
259 O << "rep/movsw (%esi),(%edi)\n";
260 return;
261 case X86::REP_STOSB:
262 O << "rep/stosb\n";
263 return;
264 case X86::REP_STOSD:
265 O << "rep/stosl\n";
266 return;
267 case X86::REP_STOSW:
268 O << "rep/stosw\n";
269 return;
270 default:
271 break;
272 }
273 }
274
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000275 // Call the autogenerated instruction printer routines.
276 printInstruction(MI);
277}
278
279// Include the auto-generated portion of the assembly writer.
280#include "X86GenAsmWriter.inc"
281