blob: e50100e7528ad7f79ff71150076e793386c517e2 [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) {
Evan Cheng3c992d22006-03-07 02:02:57 +000030 // Let PassManager know we need debug information and relay
31 // the MachineDebugInfo address on to DwarfWriter.
32 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
33
Chris Lattner8b8b9512005-11-21 07:51:23 +000034 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035 O << "\n\n";
36
Evan Cheng3c992d22006-03-07 02:02:57 +000037 // Emit pre-function debug information.
38 DW.BeginFunction(MF);
39
Chris Lattnerb36cbd02005-07-01 22:44:09 +000040 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000041 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042
43 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000044 const Function *F = MF.getFunction();
45 switch (F->getLinkage()) {
46 default: assert(0 && "Unknown linkage type!");
47 case Function::InternalLinkage: // Symbols default to internal.
48 SwitchSection(".text", F);
49 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
50 break;
51 case Function::ExternalLinkage:
52 SwitchSection(".text", F);
53 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Chris Lattner272f9982005-12-16 00:07:30 +000054 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000055 break;
56 case Function::WeakLinkage:
57 case Function::LinkOnceLinkage:
58 if (forDarwin) {
59 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
60 F);
Evan Chengf1616da2006-02-22 23:59:57 +000061 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000062 O << "\t.weak_definition\t" << CurrentFnName << "\n";
63 } else {
64 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
65 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
66 << ",\"ax\",@progbits\n";
67 O << "\t.weak " << CurrentFnName << "\n";
68 }
69 break;
70 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000071 O << CurrentFnName << ":\n";
72
73 // Print out code for the function.
74 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
75 I != E; ++I) {
76 // Print a label for the basic block.
77 if (I->pred_begin() != I->pred_end())
Chris Lattner64965ba2005-11-21 07:43:59 +000078 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
79 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
80 << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000081 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
82 II != E; ++II) {
83 // Print the assembly for the instruction.
84 O << "\t";
85 printMachineInstruction(II);
86 }
87 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000088 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000089 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000090
Evan Cheng3c992d22006-03-07 02:02:57 +000091 // Emit post-function debug information.
92 DW.EndFunction(MF);
93
Chris Lattnerb36cbd02005-07-01 22:44:09 +000094 // We didn't modify anything.
95 return false;
96}
97
Chris Lattnera3b8c572006-02-06 23:41:19 +000098void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
99 const char *Modifier) {
100 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000101 const MRegisterInfo &RI = *TM.getRegisterInfo();
102 switch (MO.getType()) {
103 case MachineOperand::MO_VirtualRegister:
104 case MachineOperand::MO_MachineRegister:
105 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
106 "Virtual registers should not make it this far!");
107 O << '%';
108 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
109 O << (char)tolower(*Name);
110 return;
111
112 case MachineOperand::MO_SignExtendedImmed:
113 case MachineOperand::MO_UnextendedImmed:
Evan Cheng3c992d22006-03-07 02:02:57 +0000114 if (!Modifier || strcmp(Modifier, "debug") != 0)
115 O << '$';
116 O << (int)MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000117 return;
118 case MachineOperand::MO_MachineBasicBlock: {
119 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner64965ba2005-11-21 07:43:59 +0000120 O << PrivateGlobalPrefix << "BB"
121 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000122 << "_" << MBBOp->getNumber () << "\t# "
123 << MBBOp->getBasicBlock ()->getName ();
124 return;
125 }
126 case MachineOperand::MO_PCRelativeDisp:
127 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
128 abort ();
129 return;
Evan Chenga09bd812006-02-26 08:28:12 +0000130 case MachineOperand::MO_ConstantPoolIndex: {
131 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
132 if (!isMemOp) O << '$';
133 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
134 << MO.getConstantPoolIndex();
135 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
136 O << "-\"L" << getFunctionNumber() << "$pb\"";
137 int Offset = MO.getOffset();
138 if (Offset > 0)
139 O << "+" << Offset;
140 else if (Offset < 0)
141 O << Offset;
142 return;
143 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000144 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000145 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000146 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000147 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng2338c5c2006-02-07 08:38:37 +0000148 // Darwin block shameless ripped from PPCAsmPrinter.cpp
Evan Cheng4c1aa862006-02-22 20:19:42 +0000149 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000150 GlobalValue *GV = MO.getGlobal();
151 std::string Name = Mang->getValueName(GV);
Evan Cheng2338c5c2006-02-07 08:38:37 +0000152 // Link-once, External, or Weakly-linked global variables need
153 // non-lazily-resolved stubs
154 if (GV->isExternal() || GV->hasWeakLinkage() ||
155 GV->hasLinkOnceLinkage()) {
156 // Dynamically-resolved functions need a stub for the function.
157 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
158 FnStubs.insert(Name);
159 O << "L" << Name << "$stub";
160 } else {
161 GVStubs.insert(Name);
162 O << "L" << Name << "$non_lazy_ptr";
163 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000164 } else {
165 O << Mang->getValueName(GV);
Evan Chenga0ea0532006-02-23 02:43:52 +0000166 }
167 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
168 O << "-\"L" << getFunctionNumber() << "$pb\"";
169 } else
Evan Cheng7ccced62006-02-18 00:15:05 +0000170 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000171 int Offset = MO.getOffset();
172 if (Offset > 0)
173 O << "+" << Offset;
174 else if (Offset < 0)
175 O << Offset;
176 return;
177 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000178 case MachineOperand::MO_ExternalSymbol: {
179 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000180 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
181 std::string Name(GlobalPrefix);
182 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000183 FnStubs.insert(Name);
184 O << "L" << Name << "$stub";
185 return;
186 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000187 if (!isCallOp) O << '$';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000188 O << GlobalPrefix << MO.getSymbolName();
189 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000190 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000191 default:
192 O << "<unknown operand type>"; return;
193 }
194}
195
Nate Begeman391c5d22005-11-30 18:54:35 +0000196void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000197 unsigned char value = MI->getOperand(Op).getImmedValue();
198 assert(value <= 7 && "Invalid ssecc argument!");
199 switch (value) {
200 case 0: O << "eq"; break;
201 case 1: O << "lt"; break;
202 case 2: O << "le"; break;
203 case 3: O << "unord"; break;
204 case 4: O << "neq"; break;
205 case 5: O << "nlt"; break;
206 case 6: O << "nle"; break;
207 case 7: O << "ord"; break;
208 }
209}
210
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000211void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
212 assert(isMem(MI, Op) && "Invalid memory reference!");
213
214 const MachineOperand &BaseReg = MI->getOperand(Op);
215 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
216 const MachineOperand &IndexReg = MI->getOperand(Op+2);
217 const MachineOperand &DispSpec = MI->getOperand(Op+3);
218
219 if (BaseReg.isFrameIndex()) {
220 O << "[frame slot #" << BaseReg.getFrameIndex();
221 if (DispSpec.getImmedValue())
222 O << " + " << DispSpec.getImmedValue();
223 O << "]";
224 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000225 }
226
Evan Chenga09bd812006-02-26 08:28:12 +0000227 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000228 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000229 } else {
230 int DispVal = DispSpec.getImmedValue();
231 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
232 O << DispVal;
233 }
234
235 if (IndexReg.getReg() || BaseReg.getReg()) {
236 O << "(";
237 if (BaseReg.getReg())
Chris Lattnera3b8c572006-02-06 23:41:19 +0000238 printOperand(MI, Op);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000239
240 if (IndexReg.getReg()) {
241 O << ",";
Chris Lattnera3b8c572006-02-06 23:41:19 +0000242 printOperand(MI, Op+2);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 if (ScaleVal != 1)
244 O << "," << ScaleVal;
245 }
246
247 O << ")";
248 }
249}
250
Evan Cheng7ccced62006-02-18 00:15:05 +0000251void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
252 O << "\"L" << getFunctionNumber() << "$pb\"\n";
253 O << "\"L" << getFunctionNumber() << "$pb\":";
254}
255
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000256/// printMachineInstruction -- Print out a single X86 LLVM instruction
257/// MI in Intel syntax to the current output stream.
258///
259void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
260 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000261 // This works around some Darwin assembler bugs.
262 if (forDarwin) {
263 switch (MI->getOpcode()) {
264 case X86::REP_MOVSB:
265 O << "rep/movsb (%esi),(%edi)\n";
266 return;
267 case X86::REP_MOVSD:
268 O << "rep/movsl (%esi),(%edi)\n";
269 return;
270 case X86::REP_MOVSW:
271 O << "rep/movsw (%esi),(%edi)\n";
272 return;
273 case X86::REP_STOSB:
274 O << "rep/stosb\n";
275 return;
276 case X86::REP_STOSD:
277 O << "rep/stosl\n";
278 return;
279 case X86::REP_STOSW:
280 O << "rep/stosw\n";
281 return;
282 default:
283 break;
284 }
285 }
286
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000287 // Call the autogenerated instruction printer routines.
288 printInstruction(MI);
289}
290
291// Include the auto-generated portion of the assembly writer.
292#include "X86GenAsmWriter.inc"
293