blob: ff96e05127cb240a8810e1126e7a02ad8aa5a477 [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;
118 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000119 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000120 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000121 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000122 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000123 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000124 GlobalValue *GV = MO.getGlobal();
125 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000126 // Link-once, External, or Weakly-linked global variables need
127 // non-lazily-resolved stubs
128 if (GV->isExternal() || GV->hasWeakLinkage() ||
129 GV->hasLinkOnceLinkage()) {
130 // Dynamically-resolved functions need a stub for the function.
131 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
132 FnStubs.insert(Name);
133 O << "L" << Name << "$stub";
134 } else {
135 GVStubs.insert(Name);
136 O << "L" << Name << "$non_lazy_ptr";
Evan Cheng4c1aa862006-02-22 20:19:42 +0000137 if (TM.getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +0000138 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000139 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000140 } else {
141 O << Mang->getValueName(GV);
Nate Begeman72b286b2005-07-08 00:23:26 +0000142 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000143 } else
144 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000145 int Offset = MO.getOffset();
146 if (Offset > 0)
147 O << "+" << Offset;
148 else if (Offset < 0)
149 O << Offset;
150 return;
151 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000152 case MachineOperand::MO_ExternalSymbol: {
153 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000154 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
155 std::string Name(GlobalPrefix);
156 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000157 FnStubs.insert(Name);
158 O << "L" << Name << "$stub";
159 return;
160 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000161 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000162 O << GlobalPrefix << MO.getSymbolName();
163 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000164 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000165 default:
166 O << "<unknown operand type>"; return;
167 }
168}
169
Nate Begeman391c5d22005-11-30 18:54:35 +0000170void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000171 unsigned char value = MI->getOperand(Op).getImmedValue();
172 assert(value <= 7 && "Invalid ssecc argument!");
173 switch (value) {
174 case 0: O << "eq"; break;
175 case 1: O << "lt"; break;
176 case 2: O << "le"; break;
177 case 3: O << "unord"; break;
178 case 4: O << "neq"; break;
179 case 5: O << "nlt"; break;
180 case 6: O << "nle"; break;
181 case 7: O << "ord"; break;
182 }
183}
184
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000185void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
186 assert(isMem(MI, Op) && "Invalid memory reference!");
187
188 const MachineOperand &BaseReg = MI->getOperand(Op);
189 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
190 const MachineOperand &IndexReg = MI->getOperand(Op+2);
191 const MachineOperand &DispSpec = MI->getOperand(Op+3);
192
193 if (BaseReg.isFrameIndex()) {
194 O << "[frame slot #" << BaseReg.getFrameIndex();
195 if (DispSpec.getImmedValue())
196 O << " + " << DispSpec.getImmedValue();
197 O << "]";
198 return;
199 } else if (BaseReg.isConstantPoolIndex()) {
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000200 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000201 << BaseReg.getConstantPoolIndex();
Evan Cheng4c1aa862006-02-22 20:19:42 +0000202 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +0000203 O << "-\"L" << getFunctionNumber() << "$pb\"";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000204 if (DispSpec.getImmedValue())
205 O << "+" << DispSpec.getImmedValue();
206 if (IndexReg.getReg()) {
207 O << "(,";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000208 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209 if (ScaleVal != 1)
210 O << "," << ScaleVal;
211 O << ")";
212 }
213 return;
214 }
215
216 if (DispSpec.isGlobalAddress()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000217 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000218 } else {
219 int DispVal = DispSpec.getImmedValue();
220 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
221 O << DispVal;
222 }
223
224 if (IndexReg.getReg() || BaseReg.getReg()) {
225 O << "(";
226 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000227 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000228
229 if (IndexReg.getReg()) {
230 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000231 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000232 if (ScaleVal != 1)
233 O << "," << ScaleVal;
234 }
235
236 O << ")";
237 }
238}
239
Evan Cheng7ccced62006-02-18 00:15:05 +0000240void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
243}
244
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245/// printMachineInstruction -- Print out a single X86 LLVM instruction
246/// MI in Intel syntax to the current output stream.
247///
248void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
249 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000250 // This works around some Darwin assembler bugs.
251 if (forDarwin) {
252 switch (MI->getOpcode()) {
253 case X86::REP_MOVSB:
254 O << "rep/movsb (%esi),(%edi)\n";
255 return;
256 case X86::REP_MOVSD:
257 O << "rep/movsl (%esi),(%edi)\n";
258 return;
259 case X86::REP_MOVSW:
260 O << "rep/movsw (%esi),(%edi)\n";
261 return;
262 case X86::REP_STOSB:
263 O << "rep/stosb\n";
264 return;
265 case X86::REP_STOSD:
266 O << "rep/stosl\n";
267 return;
268 case X86::REP_STOSW:
269 O << "rep/stosw\n";
270 return;
271 default:
272 break;
273 }
274 }
275
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000276 // Call the autogenerated instruction printer routines.
277 printInstruction(MI);
278}
279
280// Include the auto-generated portion of the assembly writer.
281#include "X86GenAsmWriter.inc"
282