blob: 353cd5460f9c7bc6defe20220ab137c0f727b708 [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"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000021#include <iostream>
Chris Lattnerb36cbd02005-07-01 22:44:09 +000022using namespace llvm;
23using namespace x86;
24
25/// runOnMachineFunction - This uses the printMachineInstruction()
26/// method to print assembly for each instruction.
27///
28bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +000029 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030 O << "\n\n";
31
32 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000033 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034
35 // Print out labels for the function.
Chris Lattner7b6e53c2005-11-21 07:16:34 +000036 SwitchSection("\t.text\n", MF.getFunction());
Chris Lattner8b8b9512005-11-21 07:51:23 +000037 EmitAlignment(4); // FIXME: This should be parameterized somewhere.
Chris Lattner272f9982005-12-16 00:07:30 +000038 if (!MF.getFunction()->hasInternalLinkage())
39 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnerac2902b2005-11-21 23:06:54 +000040 if (HasDotTypeDotSizeDirective)
Chris Lattnerb36cbd02005-07-01 22:44:09 +000041 O << "\t.type\t" << CurrentFnName << ", @function\n";
42 O << CurrentFnName << ":\n";
43
44 // Print out code for the function.
45 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
46 I != E; ++I) {
47 // Print a label for the basic block.
48 if (I->pred_begin() != I->pred_end())
Chris Lattner64965ba2005-11-21 07:43:59 +000049 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
50 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
51 << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000052 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
53 II != E; ++II) {
54 // Print the assembly for the instruction.
55 O << "\t";
56 printMachineInstruction(II);
57 }
58 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000059 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000060 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000061
62 // We didn't modify anything.
63 return false;
64}
65
Chris Lattnera3b8c572006-02-06 23:41:19 +000066void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
67 const char *Modifier) {
68 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000069 const MRegisterInfo &RI = *TM.getRegisterInfo();
70 switch (MO.getType()) {
71 case MachineOperand::MO_VirtualRegister:
72 case MachineOperand::MO_MachineRegister:
73 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
74 "Virtual registers should not make it this far!");
75 O << '%';
76 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
77 O << (char)tolower(*Name);
78 return;
79
80 case MachineOperand::MO_SignExtendedImmed:
81 case MachineOperand::MO_UnextendedImmed:
82 O << '$' << (int)MO.getImmedValue();
83 return;
84 case MachineOperand::MO_MachineBasicBlock: {
85 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner64965ba2005-11-21 07:43:59 +000086 O << PrivateGlobalPrefix << "BB"
87 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb36cbd02005-07-01 22:44:09 +000088 << "_" << MBBOp->getNumber () << "\t# "
89 << MBBOp->getBasicBlock ()->getName ();
90 return;
91 }
92 case MachineOperand::MO_PCRelativeDisp:
93 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
94 abort ();
95 return;
96 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +000097 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Nate Begeman72b286b2005-07-08 00:23:26 +000098 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
99 if (forDarwin) {
100 if (!isCallOp) O << '$';
101 GlobalValue *GV = MO.getGlobal();
102 std::string Name = Mang->getValueName(GV);
103
104 // Dynamically-resolved functions need a stub for the function. Be
105 // wary however not to output $stub for external functions whose addresses
106 // are taken. Those should be emitted as $non_lazy_ptr below.
107 Function *F = dyn_cast<Function>(GV);
108 if (F && isCallOp && F->isExternal()) {
109 FnStubs.insert(Name);
110 O << "L" << Name << "$stub";
Nate Begemand3a490a2005-07-12 18:34:58 +0000111 } else if (GV->hasLinkOnceLinkage()) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000112 // Link-once, External, or Weakly-linked global variables need
Nate Begemand3a490a2005-07-12 18:34:58 +0000113 // non-lazily-resolved stubs
Nate Begeman72b286b2005-07-08 00:23:26 +0000114 LinkOnceStubs.insert(Name);
115 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000116 } else if (GV->isExternal() || GV->hasWeakLinkage()) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000117 GVStubs.insert(Name);
118 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000119 } else {
120 O << Mang->getValueName(GV);
Nate Begeman72b286b2005-07-08 00:23:26 +0000121 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000122 int Offset = MO.getOffset();
123 if (Offset > 0)
124 O << "+" << Offset;
125 else if (Offset < 0)
126 O << Offset;
Nate Begeman72b286b2005-07-08 00:23:26 +0000127 return;
128 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000129 if (!isCallOp) O << '$';
130 O << Mang->getValueName(MO.getGlobal());
131 int Offset = MO.getOffset();
132 if (Offset > 0)
133 O << "+" << Offset;
134 else if (Offset < 0)
135 O << Offset;
136 return;
137 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000138 case MachineOperand::MO_ExternalSymbol: {
139 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Nate Begeman72b286b2005-07-08 00:23:26 +0000140 if (isCallOp && forDarwin) {
141 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
142 FnStubs.insert(Name);
143 O << "L" << Name << "$stub";
144 return;
145 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000146 if (!isCallOp) O << '$';
147 O << GlobalPrefix << MO.getSymbolName();
148 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000149 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000150 default:
151 O << "<unknown operand type>"; return;
152 }
153}
154
Nate Begeman391c5d22005-11-30 18:54:35 +0000155void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000156 unsigned char value = MI->getOperand(Op).getImmedValue();
157 assert(value <= 7 && "Invalid ssecc argument!");
158 switch (value) {
159 case 0: O << "eq"; break;
160 case 1: O << "lt"; break;
161 case 2: O << "le"; break;
162 case 3: O << "unord"; break;
163 case 4: O << "neq"; break;
164 case 5: O << "nlt"; break;
165 case 6: O << "nle"; break;
166 case 7: O << "ord"; break;
167 }
168}
169
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000170void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
171 assert(isMem(MI, Op) && "Invalid memory reference!");
172
173 const MachineOperand &BaseReg = MI->getOperand(Op);
174 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
175 const MachineOperand &IndexReg = MI->getOperand(Op+2);
176 const MachineOperand &DispSpec = MI->getOperand(Op+3);
177
178 if (BaseReg.isFrameIndex()) {
179 O << "[frame slot #" << BaseReg.getFrameIndex();
180 if (DispSpec.getImmedValue())
181 O << " + " << DispSpec.getImmedValue();
182 O << "]";
183 return;
184 } else if (BaseReg.isConstantPoolIndex()) {
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000185 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000186 << BaseReg.getConstantPoolIndex();
187 if (DispSpec.getImmedValue())
188 O << "+" << DispSpec.getImmedValue();
189 if (IndexReg.getReg()) {
190 O << "(,";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000191 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000192 if (ScaleVal != 1)
193 O << "," << ScaleVal;
194 O << ")";
195 }
196 return;
197 }
198
199 if (DispSpec.isGlobalAddress()) {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000200 printOperand(MI, Op+3, "call");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000201 } else {
202 int DispVal = DispSpec.getImmedValue();
203 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
204 O << DispVal;
205 }
206
207 if (IndexReg.getReg() || BaseReg.getReg()) {
208 O << "(";
209 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000210 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000211
212 if (IndexReg.getReg()) {
213 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000214 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000215 if (ScaleVal != 1)
216 O << "," << ScaleVal;
217 }
218
219 O << ")";
220 }
221}
222
223/// printMachineInstruction -- Print out a single X86 LLVM instruction
224/// MI in Intel syntax to the current output stream.
225///
226void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
227 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000228 // This works around some Darwin assembler bugs.
229 if (forDarwin) {
230 switch (MI->getOpcode()) {
231 case X86::REP_MOVSB:
232 O << "rep/movsb (%esi),(%edi)\n";
233 return;
234 case X86::REP_MOVSD:
235 O << "rep/movsl (%esi),(%edi)\n";
236 return;
237 case X86::REP_MOVSW:
238 O << "rep/movsw (%esi),(%edi)\n";
239 return;
240 case X86::REP_STOSB:
241 O << "rep/stosb\n";
242 return;
243 case X86::REP_STOSD:
244 O << "rep/stosl\n";
245 return;
246 case X86::REP_STOSW:
247 O << "rep/stosw\n";
248 return;
249 default:
250 break;
251 }
252 }
253
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000254 // Call the autogenerated instruction printer routines.
255 printInstruction(MI);
256}
257
258// Include the auto-generated portion of the assembly writer.
259#include "X86GenAsmWriter.inc"
260