blob: e9da1855ddd5b86d1acdbc576bd72f589044b39b [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);
Evan Chengf1616da2006-02-22 23:59:57 +000054 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000055 O << "\t.weak_definition\t" << CurrentFnName << "\n";
56 } else {
57 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
58 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
59 << ",\"ax\",@progbits\n";
60 O << "\t.weak " << CurrentFnName << "\n";
61 }
62 break;
63 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000064 O << CurrentFnName << ":\n";
65
66 // Print out code for the function.
67 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
68 I != E; ++I) {
69 // Print a label for the basic block.
70 if (I->pred_begin() != I->pred_end())
Chris Lattner64965ba2005-11-21 07:43:59 +000071 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
72 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
73 << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000074 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
75 II != E; ++II) {
76 // Print the assembly for the instruction.
77 O << "\t";
78 printMachineInstruction(II);
79 }
80 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000081 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000082 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000083
84 // We didn't modify anything.
85 return false;
86}
87
Chris Lattnera3b8c572006-02-06 23:41:19 +000088void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
89 const char *Modifier) {
90 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000091 const MRegisterInfo &RI = *TM.getRegisterInfo();
92 switch (MO.getType()) {
93 case MachineOperand::MO_VirtualRegister:
94 case MachineOperand::MO_MachineRegister:
95 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
96 "Virtual registers should not make it this far!");
97 O << '%';
98 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
99 O << (char)tolower(*Name);
100 return;
101
102 case MachineOperand::MO_SignExtendedImmed:
103 case MachineOperand::MO_UnextendedImmed:
104 O << '$' << (int)MO.getImmedValue();
105 return;
106 case MachineOperand::MO_MachineBasicBlock: {
107 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner64965ba2005-11-21 07:43:59 +0000108 O << PrivateGlobalPrefix << "BB"
109 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000110 << "_" << MBBOp->getNumber () << "\t# "
111 << MBBOp->getBasicBlock ()->getName ();
112 return;
113 }
114 case MachineOperand::MO_PCRelativeDisp:
115 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
116 abort ();
117 return;
Evan Chenga09bd812006-02-26 08:28:12 +0000118 case MachineOperand::MO_ConstantPoolIndex: {
119 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
120 if (!isMemOp) O << '$';
121 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
122 << MO.getConstantPoolIndex();
123 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
124 O << "-\"L" << getFunctionNumber() << "$pb\"";
125 int Offset = MO.getOffset();
126 if (Offset > 0)
127 O << "+" << Offset;
128 else if (Offset < 0)
129 O << Offset;
130 return;
131 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000132 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000133 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000134 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000135 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000136 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000137 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000138 GlobalValue *GV = MO.getGlobal();
139 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000140 // Link-once, External, or Weakly-linked global variables need
141 // non-lazily-resolved stubs
142 if (GV->isExternal() || GV->hasWeakLinkage() ||
143 GV->hasLinkOnceLinkage()) {
144 // Dynamically-resolved functions need a stub for the function.
145 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
146 FnStubs.insert(Name);
147 O << "L" << Name << "$stub";
148 } else {
149 GVStubs.insert(Name);
150 O << "L" << Name << "$non_lazy_ptr";
151 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000152 } else {
153 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000154 }
155 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
156 O << "-\"L" << getFunctionNumber() << "$pb\"";
157 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000158 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000159 int Offset = MO.getOffset();
160 if (Offset > 0)
161 O << "+" << Offset;
162 else if (Offset < 0)
163 O << Offset;
164 return;
165 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000166 case MachineOperand::MO_ExternalSymbol: {
167 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000168 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
169 std::string Name(GlobalPrefix);
170 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000171 FnStubs.insert(Name);
172 O << "L" << Name << "$stub";
173 return;
174 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000175 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000176 O << GlobalPrefix << MO.getSymbolName();
177 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000178 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000179 default:
180 O << "<unknown operand type>"; return;
181 }
182}
183
Nate Begeman391c5d22005-11-30 18:54:35 +0000184void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000185 unsigned char value = MI->getOperand(Op).getImmedValue();
186 assert(value <= 7 && "Invalid ssecc argument!");
187 switch (value) {
188 case 0: O << "eq"; break;
189 case 1: O << "lt"; break;
190 case 2: O << "le"; break;
191 case 3: O << "unord"; break;
192 case 4: O << "neq"; break;
193 case 5: O << "nlt"; break;
194 case 6: O << "nle"; break;
195 case 7: O << "ord"; break;
196 }
197}
198
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000199void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
200 assert(isMem(MI, Op) && "Invalid memory reference!");
201
202 const MachineOperand &BaseReg = MI->getOperand(Op);
203 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
204 const MachineOperand &IndexReg = MI->getOperand(Op+2);
205 const MachineOperand &DispSpec = MI->getOperand(Op+3);
206
207 if (BaseReg.isFrameIndex()) {
208 O << "[frame slot #" << BaseReg.getFrameIndex();
209 if (DispSpec.getImmedValue())
210 O << " + " << DispSpec.getImmedValue();
211 O << "]";
212 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213 }
214
Evan Chenga09bd812006-02-26 08:28:12 +0000215 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
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