blob: bf4dba739340255c4f684527dfff18c094c9434e [file] [log] [blame]
Chris Lattnerb9740462005-07-01 22:44:09 +00001//===-- X86IntelAsmPrinter.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 Intel format assembly language.
12// This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86IntelAsmPrinter.h"
17#include "X86.h"
18#include "llvm/Module.h"
19#include "llvm/Assembly/Writer.h"
20#include "llvm/Support/Mangler.h"
Evan Cheng5588de92006-02-18 00:15:05 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000022using namespace llvm;
Chris Lattnerb9740462005-07-01 22:44:09 +000023
24/// runOnMachineFunction - This uses the printMachineInstruction()
25/// method to print assembly for each instruction.
26///
27bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga4a4ceb2006-03-07 02:23:26 +000028 if (forDarwin) {
29 // Let PassManager know we need debug information and relay
30 // the MachineDebugInfo address on to DwarfWriter.
31 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
32 }
Evan Cheng30d7b702006-03-07 02:02:57 +000033
Chris Lattner99946fb2005-11-21 07:51:23 +000034 SetupMachineFunction(MF);
Chris Lattnerb9740462005-07-01 22:44:09 +000035 O << "\n\n";
36
37 // Print out constants referenced by the function
Chris Lattner8a5f3c12005-11-21 08:32:23 +000038 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb9740462005-07-01 22:44:09 +000039
40 // Print out labels for the function.
Chris Lattner050bf2f2005-11-21 07:16:34 +000041 SwitchSection("\t.text\n", MF.getFunction());
Chris Lattner99946fb2005-11-21 07:51:23 +000042 EmitAlignment(4);
Chris Lattnerb9740462005-07-01 22:44:09 +000043 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnerac6cb462005-11-21 23:06:54 +000044 if (HasDotTypeDotSizeDirective)
Chris Lattnerb9740462005-07-01 22:44:09 +000045 O << "\t.type\t" << CurrentFnName << ", @function\n";
46 O << CurrentFnName << ":\n";
Jim Laskeyc0d65182006-04-07 20:44:42 +000047
48 if (forDarwin) {
49 // Emit pre-function debug information.
50 DW.BeginFunction(&MF);
51 }
Chris Lattnerb9740462005-07-01 22:44:09 +000052
53 // Print out code for the function.
54 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
55 I != E; ++I) {
56 // Print a label for the basic block if there are any predecessors.
57 if (I->pred_begin() != I->pred_end())
Chris Lattnerd3656272005-11-21 07:43:59 +000058 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
59 << ":\t"
Chris Lattnerb9740462005-07-01 22:44:09 +000060 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
61 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
62 II != E; ++II) {
63 // Print the assembly for the instruction.
64 O << "\t";
65 printMachineInstruction(II);
66 }
67 }
68
Evan Chenga4a4ceb2006-03-07 02:23:26 +000069 if (forDarwin) {
70 // Emit post-function debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +000071 DW.EndFunction();
Evan Chenga4a4ceb2006-03-07 02:23:26 +000072 }
Evan Cheng30d7b702006-03-07 02:02:57 +000073
Chris Lattnerb9740462005-07-01 22:44:09 +000074 // We didn't modify anything.
75 return false;
76}
77
Nate Begeman6f8c1ac2005-11-30 18:54:35 +000078void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman0f38dc42005-07-14 22:52:25 +000079 unsigned char value = MI->getOperand(Op).getImmedValue();
80 assert(value <= 7 && "Invalid ssecc argument!");
81 switch (value) {
82 case 0: O << "eq"; break;
83 case 1: O << "lt"; break;
84 case 2: O << "le"; break;
85 case 3: O << "unord"; break;
86 case 4: O << "neq"; break;
87 case 5: O << "nlt"; break;
88 case 6: O << "nle"; break;
89 case 7: O << "ord"; break;
90 }
91}
92
Chris Lattnerd62a3bf2006-02-06 23:41:19 +000093void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
94 const char *Modifier) {
Chris Lattnerb9740462005-07-01 22:44:09 +000095 const MRegisterInfo &RI = *TM.getRegisterInfo();
96 switch (MO.getType()) {
97 case MachineOperand::MO_VirtualRegister:
98 if (Value *V = MO.getVRegValueOrNull()) {
99 O << "<" << V->getName() << ">";
100 return;
101 }
102 // FALLTHROUGH
103 case MachineOperand::MO_MachineRegister:
104 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
105 // Bug Workaround: See note in Printer::doInitialization about %.
106 O << "%" << RI.get(MO.getReg()).Name;
107 else
108 O << "%reg" << MO.getReg();
109 return;
110
111 case MachineOperand::MO_SignExtendedImmed:
112 case MachineOperand::MO_UnextendedImmed:
113 O << (int)MO.getImmedValue();
114 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000115 case MachineOperand::MO_MachineBasicBlock:
116 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb9740462005-07-01 22:44:09 +0000117 return;
Chris Lattnerb9740462005-07-01 22:44:09 +0000118 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerde02d772006-01-22 23:41:00 +0000119 assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
Chris Lattnerb9740462005-07-01 22:44:09 +0000120 abort ();
121 return;
Evan Cheng75b87832006-02-26 08:28:12 +0000122 case MachineOperand::MO_ConstantPoolIndex: {
123 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
124 if (!isMemOp) O << "OFFSET ";
125 O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
126 << MO.getConstantPoolIndex();
127 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
128 O << "-\"L" << getFunctionNumber() << "$pb\"";
129 int Offset = MO.getOffset();
130 if (Offset > 0)
131 O << " + " << Offset;
132 else if (Offset < 0)
133 O << Offset;
134 O << "]";
135 return;
136 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000137 case MachineOperand::MO_GlobalAddress: {
Evan Cheng5588de92006-02-18 00:15:05 +0000138 bool isCallOp = Modifier && !strcmp(Modifier, "call");
139 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
140 if (!isMemOp && !isCallOp) O << "OFFSET ";
Evan Cheng73136df2006-02-22 20:19:42 +0000141 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Evan Cheng5588de92006-02-18 00:15:05 +0000142 GlobalValue *GV = MO.getGlobal();
143 std::string Name = Mang->getValueName(GV);
144 if (!isMemOp && !isCallOp) O << '$';
145 // Link-once, External, or Weakly-linked global variables need
146 // non-lazily-resolved stubs
147 if (GV->isExternal() || GV->hasWeakLinkage() ||
148 GV->hasLinkOnceLinkage()) {
149 // Dynamically-resolved functions need a stub for the function.
150 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
151 FnStubs.insert(Name);
152 O << "L" << Name << "$stub";
153 } else {
154 GVStubs.insert(Name);
155 O << "L" << Name << "$non_lazy_ptr";
Evan Cheng5588de92006-02-18 00:15:05 +0000156 }
157 } else {
158 O << Mang->getValueName(GV);
159 }
Evan Cheng1f342c22006-02-23 02:43:52 +0000160 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
161 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng5588de92006-02-18 00:15:05 +0000162 } else
163 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb9740462005-07-01 22:44:09 +0000164 int Offset = MO.getOffset();
165 if (Offset > 0)
166 O << " + " << Offset;
167 else if (Offset < 0)
Evan Chengd2cb7052005-11-30 01:59:00 +0000168 O << Offset;
Chris Lattnerb9740462005-07-01 22:44:09 +0000169 return;
170 }
Evan Cheng5588de92006-02-18 00:15:05 +0000171 case MachineOperand::MO_ExternalSymbol: {
172 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng73136df2006-02-22 20:19:42 +0000173 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
174 std::string Name(GlobalPrefix);
175 Name += MO.getSymbolName();
Evan Cheng5588de92006-02-18 00:15:05 +0000176 FnStubs.insert(Name);
177 O << "L" << Name << "$stub";
178 return;
179 }
Evan Cheng73136df2006-02-22 20:19:42 +0000180 if (!isCallOp) O << "OFFSET ";
Chris Lattnerb9740462005-07-01 22:44:09 +0000181 O << GlobalPrefix << MO.getSymbolName();
182 return;
Evan Cheng5588de92006-02-18 00:15:05 +0000183 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000184 default:
185 O << "<unknown operand type>"; return;
186 }
187}
188
189void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
190 assert(isMem(MI, Op) && "Invalid memory reference!");
191
192 const MachineOperand &BaseReg = MI->getOperand(Op);
193 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
194 const MachineOperand &IndexReg = MI->getOperand(Op+2);
195 const MachineOperand &DispSpec = MI->getOperand(Op+3);
196
197 if (BaseReg.isFrameIndex()) {
198 O << "[frame slot #" << BaseReg.getFrameIndex();
199 if (DispSpec.getImmedValue())
200 O << " + " << DispSpec.getImmedValue();
201 O << "]";
202 return;
Chris Lattnerb9740462005-07-01 22:44:09 +0000203 }
204
205 O << "[";
206 bool NeedPlus = false;
207 if (BaseReg.getReg()) {
Evan Cheng5a766802006-02-07 08:38:37 +0000208 printOp(BaseReg, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000209 NeedPlus = true;
210 }
211
212 if (IndexReg.getReg()) {
213 if (NeedPlus) O << " + ";
214 if (ScaleVal != 1)
215 O << ScaleVal << "*";
216 printOp(IndexReg);
217 NeedPlus = true;
218 }
219
Evan Cheng75b87832006-02-26 08:28:12 +0000220 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Chris Lattnerb9740462005-07-01 22:44:09 +0000221 if (NeedPlus)
222 O << " + ";
Evan Cheng5a766802006-02-07 08:38:37 +0000223 printOp(DispSpec, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000224 } else {
225 int DispVal = DispSpec.getImmedValue();
226 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
227 if (NeedPlus)
228 if (DispVal > 0)
229 O << " + ";
230 else {
231 O << " - ";
232 DispVal = -DispVal;
233 }
234 O << DispVal;
235 }
236 }
237 O << "]";
238}
239
Evan Cheng5588de92006-02-18 00:15:05 +0000240void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
243}
Chris Lattnerb9740462005-07-01 22:44:09 +0000244
Evan Cheng68a44dc2006-04-28 21:19:05 +0000245/// PrintAsmOperand - Print out an operand for an inline asm expression.
246///
247bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
248 unsigned AsmVariant,
249 const char *ExtraCode) {
250 // Does this asm operand have a single letter operand modifier?
251 if (ExtraCode && ExtraCode[0]) {
252 if (ExtraCode[1] != 0) return true; // Unknown modifier.
253
254 switch (ExtraCode[0]) {
255 default: return true; // Unknown modifier.
256 }
257 }
258
259 printOperand(MI, OpNo);
260 return false;
261}
262
263bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
264 unsigned OpNo,
265 unsigned AsmVariant,
266 const char *ExtraCode) {
267 if (ExtraCode && ExtraCode[0])
268 return true; // Unknown modifier.
269 printMemReference(MI, OpNo);
270 return false;
271}
272
Chris Lattnerb9740462005-07-01 22:44:09 +0000273/// printMachineInstruction -- Print out a single X86 LLVM instruction
274/// MI in Intel syntax to the current output stream.
275///
276void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
277 ++EmittedInsts;
278
279 // Call the autogenerated instruction printer routines.
280 printInstruction(MI);
281}
282
283bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner9f6ce0e2005-07-03 17:34:39 +0000284 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerb9740462005-07-01 22:44:09 +0000285 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
286 //
287 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
288 // instruction as a reference to the register named sp, and if you try to
289 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
290 // before being looked up in the symbol table. This creates spurious
291 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
292 // mode, and decorate all register names with percent signs.
293 O << "\t.intel_syntax\n";
294 return false;
295}
296
297// Include the auto-generated portion of the assembly writer.
298#include "X86GenAsmWriter1.inc"