blob: c4639b3151045e6ecb383c82e49a1e92ddb08c0e [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
66void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
67 const MRegisterInfo &RI = *TM.getRegisterInfo();
68 switch (MO.getType()) {
69 case MachineOperand::MO_VirtualRegister:
70 case MachineOperand::MO_MachineRegister:
71 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
72 "Virtual registers should not make it this far!");
73 O << '%';
74 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
75 O << (char)tolower(*Name);
76 return;
77
78 case MachineOperand::MO_SignExtendedImmed:
79 case MachineOperand::MO_UnextendedImmed:
80 O << '$' << (int)MO.getImmedValue();
81 return;
82 case MachineOperand::MO_MachineBasicBlock: {
83 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner64965ba2005-11-21 07:43:59 +000084 O << PrivateGlobalPrefix << "BB"
85 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb36cbd02005-07-01 22:44:09 +000086 << "_" << MBBOp->getNumber () << "\t# "
87 << MBBOp->getBasicBlock ()->getName ();
88 return;
89 }
90 case MachineOperand::MO_PCRelativeDisp:
91 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
92 abort ();
93 return;
94 case MachineOperand::MO_GlobalAddress: {
Nate Begeman72b286b2005-07-08 00:23:26 +000095 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
96 if (forDarwin) {
97 if (!isCallOp) O << '$';
98 GlobalValue *GV = MO.getGlobal();
99 std::string Name = Mang->getValueName(GV);
100
101 // Dynamically-resolved functions need a stub for the function. Be
102 // wary however not to output $stub for external functions whose addresses
103 // are taken. Those should be emitted as $non_lazy_ptr below.
104 Function *F = dyn_cast<Function>(GV);
105 if (F && isCallOp && F->isExternal()) {
106 FnStubs.insert(Name);
107 O << "L" << Name << "$stub";
Nate Begemand3a490a2005-07-12 18:34:58 +0000108 } else if (GV->hasLinkOnceLinkage()) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000109 // Link-once, External, or Weakly-linked global variables need
Nate Begemand3a490a2005-07-12 18:34:58 +0000110 // non-lazily-resolved stubs
Nate Begeman72b286b2005-07-08 00:23:26 +0000111 LinkOnceStubs.insert(Name);
112 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000113 } else if (GV->isExternal() || GV->hasWeakLinkage()) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000114 GVStubs.insert(Name);
115 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000116 } else {
117 O << Mang->getValueName(GV);
Nate Begeman72b286b2005-07-08 00:23:26 +0000118 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000119 int Offset = MO.getOffset();
120 if (Offset > 0)
121 O << "+" << Offset;
122 else if (Offset < 0)
123 O << Offset;
Nate Begeman72b286b2005-07-08 00:23:26 +0000124 return;
125 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000126 if (!isCallOp) O << '$';
127 O << Mang->getValueName(MO.getGlobal());
128 int Offset = MO.getOffset();
129 if (Offset > 0)
130 O << "+" << Offset;
131 else if (Offset < 0)
132 O << Offset;
133 return;
134 }
135 case MachineOperand::MO_ExternalSymbol:
Nate Begeman72b286b2005-07-08 00:23:26 +0000136 if (isCallOp && forDarwin) {
137 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
138 FnStubs.insert(Name);
139 O << "L" << Name << "$stub";
140 return;
141 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000142 if (!isCallOp) O << '$';
143 O << GlobalPrefix << MO.getSymbolName();
144 return;
145 default:
146 O << "<unknown operand type>"; return;
147 }
148}
149
Nate Begeman391c5d22005-11-30 18:54:35 +0000150void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000151 unsigned char value = MI->getOperand(Op).getImmedValue();
152 assert(value <= 7 && "Invalid ssecc argument!");
153 switch (value) {
154 case 0: O << "eq"; break;
155 case 1: O << "lt"; break;
156 case 2: O << "le"; break;
157 case 3: O << "unord"; break;
158 case 4: O << "neq"; break;
159 case 5: O << "nlt"; break;
160 case 6: O << "nle"; break;
161 case 7: O << "ord"; break;
162 }
163}
164
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000165void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
166 assert(isMem(MI, Op) && "Invalid memory reference!");
167
168 const MachineOperand &BaseReg = MI->getOperand(Op);
169 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
170 const MachineOperand &IndexReg = MI->getOperand(Op+2);
171 const MachineOperand &DispSpec = MI->getOperand(Op+3);
172
173 if (BaseReg.isFrameIndex()) {
174 O << "[frame slot #" << BaseReg.getFrameIndex();
175 if (DispSpec.getImmedValue())
176 O << " + " << DispSpec.getImmedValue();
177 O << "]";
178 return;
179 } else if (BaseReg.isConstantPoolIndex()) {
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000180 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000181 << BaseReg.getConstantPoolIndex();
182 if (DispSpec.getImmedValue())
183 O << "+" << DispSpec.getImmedValue();
184 if (IndexReg.getReg()) {
185 O << "(,";
186 printOp(IndexReg);
187 if (ScaleVal != 1)
188 O << "," << ScaleVal;
189 O << ")";
190 }
191 return;
192 }
193
194 if (DispSpec.isGlobalAddress()) {
195 printOp(DispSpec, true);
196 } else {
197 int DispVal = DispSpec.getImmedValue();
198 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
199 O << DispVal;
200 }
201
202 if (IndexReg.getReg() || BaseReg.getReg()) {
203 O << "(";
204 if (BaseReg.getReg())
205 printOp(BaseReg);
206
207 if (IndexReg.getReg()) {
208 O << ",";
209 printOp(IndexReg);
210 if (ScaleVal != 1)
211 O << "," << ScaleVal;
212 }
213
214 O << ")";
215 }
216}
217
218/// printMachineInstruction -- Print out a single X86 LLVM instruction
219/// MI in Intel syntax to the current output stream.
220///
221void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
222 ++EmittedInsts;
223 // Call the autogenerated instruction printer routines.
224 printInstruction(MI);
225}
226
227// Include the auto-generated portion of the assembly writer.
228#include "X86GenAsmWriter.inc"
229