blob: b2641073725d6b5bad74c502ecd123b0330d8db1 [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"
21using namespace llvm;
22using namespace x86;
23
24/// runOnMachineFunction - This uses the printMachineInstruction()
25/// method to print assembly for each instruction.
26///
27bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +000028 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000029 O << "\n\n";
30
31 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000032 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033
34 // Print out labels for the function.
Chris Lattner7b6e53c2005-11-21 07:16:34 +000035 SwitchSection("\t.text\n", MF.getFunction());
Chris Lattner8b8b9512005-11-21 07:51:23 +000036 EmitAlignment(4); // FIXME: This should be parameterized somewhere.
Chris Lattnerb36cbd02005-07-01 22:44:09 +000037 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnerac2902b2005-11-21 23:06:54 +000038 if (HasDotTypeDotSizeDirective)
Chris Lattnerb36cbd02005-07-01 22:44:09 +000039 O << "\t.type\t" << CurrentFnName << ", @function\n";
40 O << CurrentFnName << ":\n";
41
42 // Print out code for the function.
43 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
44 I != E; ++I) {
45 // Print a label for the basic block.
46 if (I->pred_begin() != I->pred_end())
Chris Lattner64965ba2005-11-21 07:43:59 +000047 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
48 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
49 << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000050 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
51 II != E; ++II) {
52 // Print the assembly for the instruction.
53 O << "\t";
54 printMachineInstruction(II);
55 }
56 }
Chris Lattnerac2902b2005-11-21 23:06:54 +000057 if (HasDotTypeDotSizeDirective)
Nate Begeman73213f62005-07-12 01:37:28 +000058 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000059
60 // We didn't modify anything.
61 return false;
62}
63
64void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
65 const MRegisterInfo &RI = *TM.getRegisterInfo();
66 switch (MO.getType()) {
67 case MachineOperand::MO_VirtualRegister:
68 case MachineOperand::MO_MachineRegister:
69 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
70 "Virtual registers should not make it this far!");
71 O << '%';
72 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
73 O << (char)tolower(*Name);
74 return;
75
76 case MachineOperand::MO_SignExtendedImmed:
77 case MachineOperand::MO_UnextendedImmed:
78 O << '$' << (int)MO.getImmedValue();
79 return;
80 case MachineOperand::MO_MachineBasicBlock: {
81 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner64965ba2005-11-21 07:43:59 +000082 O << PrivateGlobalPrefix << "BB"
83 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb36cbd02005-07-01 22:44:09 +000084 << "_" << MBBOp->getNumber () << "\t# "
85 << MBBOp->getBasicBlock ()->getName ();
86 return;
87 }
88 case MachineOperand::MO_PCRelativeDisp:
89 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
90 abort ();
91 return;
92 case MachineOperand::MO_GlobalAddress: {
Nate Begeman72b286b2005-07-08 00:23:26 +000093 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
94 if (forDarwin) {
95 if (!isCallOp) O << '$';
96 GlobalValue *GV = MO.getGlobal();
97 std::string Name = Mang->getValueName(GV);
98
99 // Dynamically-resolved functions need a stub for the function. Be
100 // wary however not to output $stub for external functions whose addresses
101 // are taken. Those should be emitted as $non_lazy_ptr below.
102 Function *F = dyn_cast<Function>(GV);
103 if (F && isCallOp && F->isExternal()) {
104 FnStubs.insert(Name);
105 O << "L" << Name << "$stub";
Nate Begemand3a490a2005-07-12 18:34:58 +0000106 } else if (GV->hasLinkOnceLinkage()) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000107 // Link-once, External, or Weakly-linked global variables need
Nate Begemand3a490a2005-07-12 18:34:58 +0000108 // non-lazily-resolved stubs
Nate Begeman72b286b2005-07-08 00:23:26 +0000109 LinkOnceStubs.insert(Name);
110 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000111 } else if (GV->isExternal() || GV->hasWeakLinkage()) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000112 GVStubs.insert(Name);
113 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000114 } else {
115 O << Mang->getValueName(GV);
Nate Begeman72b286b2005-07-08 00:23:26 +0000116 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000117 int Offset = MO.getOffset();
118 if (Offset > 0)
119 O << "+" << Offset;
120 else if (Offset < 0)
121 O << Offset;
Nate Begeman72b286b2005-07-08 00:23:26 +0000122 return;
123 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000124 if (!isCallOp) O << '$';
125 O << Mang->getValueName(MO.getGlobal());
126 int Offset = MO.getOffset();
127 if (Offset > 0)
128 O << "+" << Offset;
129 else if (Offset < 0)
130 O << Offset;
131 return;
132 }
133 case MachineOperand::MO_ExternalSymbol:
Nate Begeman72b286b2005-07-08 00:23:26 +0000134 if (isCallOp && forDarwin) {
135 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
136 FnStubs.insert(Name);
137 O << "L" << Name << "$stub";
138 return;
139 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000140 if (!isCallOp) O << '$';
141 O << GlobalPrefix << MO.getSymbolName();
142 return;
143 default:
144 O << "<unknown operand type>"; return;
145 }
146}
147
Nate Begeman6c7cb292005-07-14 22:52:25 +0000148void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
149 MVT::ValueType VT) {
150 unsigned char value = MI->getOperand(Op).getImmedValue();
151 assert(value <= 7 && "Invalid ssecc argument!");
152 switch (value) {
153 case 0: O << "eq"; break;
154 case 1: O << "lt"; break;
155 case 2: O << "le"; break;
156 case 3: O << "unord"; break;
157 case 4: O << "neq"; break;
158 case 5: O << "nlt"; break;
159 case 6: O << "nle"; break;
160 case 7: O << "ord"; break;
161 }
162}
163
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000164void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
165 assert(isMem(MI, Op) && "Invalid memory reference!");
166
167 const MachineOperand &BaseReg = MI->getOperand(Op);
168 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
169 const MachineOperand &IndexReg = MI->getOperand(Op+2);
170 const MachineOperand &DispSpec = MI->getOperand(Op+3);
171
172 if (BaseReg.isFrameIndex()) {
173 O << "[frame slot #" << BaseReg.getFrameIndex();
174 if (DispSpec.getImmedValue())
175 O << " + " << DispSpec.getImmedValue();
176 O << "]";
177 return;
178 } else if (BaseReg.isConstantPoolIndex()) {
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000179 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000180 << BaseReg.getConstantPoolIndex();
181 if (DispSpec.getImmedValue())
182 O << "+" << DispSpec.getImmedValue();
183 if (IndexReg.getReg()) {
184 O << "(,";
185 printOp(IndexReg);
186 if (ScaleVal != 1)
187 O << "," << ScaleVal;
188 O << ")";
189 }
190 return;
191 }
192
193 if (DispSpec.isGlobalAddress()) {
194 printOp(DispSpec, true);
195 } else {
196 int DispVal = DispSpec.getImmedValue();
197 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
198 O << DispVal;
199 }
200
201 if (IndexReg.getReg() || BaseReg.getReg()) {
202 O << "(";
203 if (BaseReg.getReg())
204 printOp(BaseReg);
205
206 if (IndexReg.getReg()) {
207 O << ",";
208 printOp(IndexReg);
209 if (ScaleVal != 1)
210 O << "," << ScaleVal;
211 }
212
213 O << ")";
214 }
215}
216
217/// printMachineInstruction -- Print out a single X86 LLVM instruction
218/// MI in Intel syntax to the current output stream.
219///
220void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
221 ++EmittedInsts;
222 // Call the autogenerated instruction printer routines.
223 printInstruction(MI);
224}
225
226// Include the auto-generated portion of the assembly writer.
227#include "X86GenAsmWriter.inc"
228