blob: 1a5aba16608497dca8e70847f66da221189ccb5f [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;
23using namespace x86;
24
25/// runOnMachineFunction - This uses the printMachineInstruction()
26/// method to print assembly for each instruction.
27///
28bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner99946fb2005-11-21 07:51:23 +000029 SetupMachineFunction(MF);
Chris Lattnerb9740462005-07-01 22:44:09 +000030 O << "\n\n";
31
32 // Print out constants referenced by the function
Chris Lattner8a5f3c12005-11-21 08:32:23 +000033 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb9740462005-07-01 22:44:09 +000034
35 // Print out labels for the function.
Chris Lattner050bf2f2005-11-21 07:16:34 +000036 SwitchSection("\t.text\n", MF.getFunction());
Chris Lattner99946fb2005-11-21 07:51:23 +000037 EmitAlignment(4);
Chris Lattnerb9740462005-07-01 22:44:09 +000038 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnerac6cb462005-11-21 23:06:54 +000039 if (HasDotTypeDotSizeDirective)
Chris Lattnerb9740462005-07-01 22:44:09 +000040 O << "\t.type\t" << CurrentFnName << ", @function\n";
41 O << CurrentFnName << ":\n";
42
43 // Print out code for the function.
44 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
45 I != E; ++I) {
46 // Print a label for the basic block if there are any predecessors.
47 if (I->pred_begin() != I->pred_end())
Chris Lattnerd3656272005-11-21 07:43:59 +000048 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
49 << ":\t"
Chris Lattnerb9740462005-07-01 22:44:09 +000050 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
51 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
52 II != E; ++II) {
53 // Print the assembly for the instruction.
54 O << "\t";
55 printMachineInstruction(II);
56 }
57 }
58
59 // We didn't modify anything.
60 return false;
61}
62
Nate Begeman6f8c1ac2005-11-30 18:54:35 +000063void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman0f38dc42005-07-14 22:52:25 +000064 unsigned char value = MI->getOperand(Op).getImmedValue();
65 assert(value <= 7 && "Invalid ssecc argument!");
66 switch (value) {
67 case 0: O << "eq"; break;
68 case 1: O << "lt"; break;
69 case 2: O << "le"; break;
70 case 3: O << "unord"; break;
71 case 4: O << "neq"; break;
72 case 5: O << "nlt"; break;
73 case 6: O << "nle"; break;
74 case 7: O << "ord"; break;
75 }
76}
77
Chris Lattnerd62a3bf2006-02-06 23:41:19 +000078void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
79 const char *Modifier) {
Chris Lattnerb9740462005-07-01 22:44:09 +000080 const MRegisterInfo &RI = *TM.getRegisterInfo();
81 switch (MO.getType()) {
82 case MachineOperand::MO_VirtualRegister:
83 if (Value *V = MO.getVRegValueOrNull()) {
84 O << "<" << V->getName() << ">";
85 return;
86 }
87 // FALLTHROUGH
88 case MachineOperand::MO_MachineRegister:
89 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
90 // Bug Workaround: See note in Printer::doInitialization about %.
91 O << "%" << RI.get(MO.getReg()).Name;
92 else
93 O << "%reg" << MO.getReg();
94 return;
95
96 case MachineOperand::MO_SignExtendedImmed:
97 case MachineOperand::MO_UnextendedImmed:
98 O << (int)MO.getImmedValue();
99 return;
100 case MachineOperand::MO_MachineBasicBlock: {
101 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattnerd3656272005-11-21 07:43:59 +0000102 O << PrivateGlobalPrefix << "BB"
103 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb9740462005-07-01 22:44:09 +0000104 << "_" << MBBOp->getNumber () << "\t# "
105 << MBBOp->getBasicBlock ()->getName ();
106 return;
107 }
108 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerde02d772006-01-22 23:41:00 +0000109 assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
Chris Lattnerb9740462005-07-01 22:44:09 +0000110 abort ();
111 return;
112 case MachineOperand::MO_GlobalAddress: {
Evan Cheng5588de92006-02-18 00:15:05 +0000113 bool isCallOp = Modifier && !strcmp(Modifier, "call");
114 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
115 if (!isMemOp && !isCallOp) O << "OFFSET ";
Evan Cheng73136df2006-02-22 20:19:42 +0000116 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Evan Cheng5588de92006-02-18 00:15:05 +0000117 GlobalValue *GV = MO.getGlobal();
118 std::string Name = Mang->getValueName(GV);
119 if (!isMemOp && !isCallOp) O << '$';
120 // Link-once, External, or Weakly-linked global variables need
121 // non-lazily-resolved stubs
122 if (GV->isExternal() || GV->hasWeakLinkage() ||
123 GV->hasLinkOnceLinkage()) {
124 // Dynamically-resolved functions need a stub for the function.
125 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
126 FnStubs.insert(Name);
127 O << "L" << Name << "$stub";
128 } else {
129 GVStubs.insert(Name);
130 O << "L" << Name << "$non_lazy_ptr";
Evan Cheng73136df2006-02-22 20:19:42 +0000131 if (TM.getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +0000132 O << "-\"L" << getFunctionNumber() << "$pb\"";
133 }
134 } else {
135 O << Mang->getValueName(GV);
136 }
137 } else
138 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb9740462005-07-01 22:44:09 +0000139 int Offset = MO.getOffset();
140 if (Offset > 0)
141 O << " + " << Offset;
142 else if (Offset < 0)
Evan Chengd2cb7052005-11-30 01:59:00 +0000143 O << Offset;
Chris Lattnerb9740462005-07-01 22:44:09 +0000144 return;
145 }
Evan Cheng5588de92006-02-18 00:15:05 +0000146 case MachineOperand::MO_ExternalSymbol: {
147 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng73136df2006-02-22 20:19:42 +0000148 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
149 std::string Name(GlobalPrefix);
150 Name += MO.getSymbolName();
Evan Cheng5588de92006-02-18 00:15:05 +0000151 FnStubs.insert(Name);
152 O << "L" << Name << "$stub";
153 return;
154 }
Evan Cheng73136df2006-02-22 20:19:42 +0000155 if (!isCallOp) O << "OFFSET ";
Chris Lattnerb9740462005-07-01 22:44:09 +0000156 O << GlobalPrefix << MO.getSymbolName();
157 return;
Evan Cheng5588de92006-02-18 00:15:05 +0000158 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000159 default:
160 O << "<unknown operand type>"; return;
161 }
162}
163
164void X86IntelAsmPrinter::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 Lattner8a5f3c12005-11-21 08:32:23 +0000179 O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb9740462005-07-01 22:44:09 +0000180 << BaseReg.getConstantPoolIndex();
Evan Cheng73136df2006-02-22 20:19:42 +0000181 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +0000182 O << "-\"L" << getFunctionNumber() << "$pb\"";
Chris Lattnerb9740462005-07-01 22:44:09 +0000183
184 if (IndexReg.getReg()) {
185 O << " + ";
186 if (ScaleVal != 1)
187 O << ScaleVal << "*";
188 printOp(IndexReg);
189 }
190
191 if (DispSpec.getImmedValue())
192 O << " + " << DispSpec.getImmedValue();
193 O << "]";
194 return;
195 }
196
197 O << "[";
198 bool NeedPlus = false;
199 if (BaseReg.getReg()) {
Evan Cheng5a766802006-02-07 08:38:37 +0000200 printOp(BaseReg, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000201 NeedPlus = true;
202 }
203
204 if (IndexReg.getReg()) {
205 if (NeedPlus) O << " + ";
206 if (ScaleVal != 1)
207 O << ScaleVal << "*";
208 printOp(IndexReg);
209 NeedPlus = true;
210 }
211
212 if (DispSpec.isGlobalAddress()) {
213 if (NeedPlus)
214 O << " + ";
Evan Cheng5a766802006-02-07 08:38:37 +0000215 printOp(DispSpec, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000216 } else {
217 int DispVal = DispSpec.getImmedValue();
218 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
219 if (NeedPlus)
220 if (DispVal > 0)
221 O << " + ";
222 else {
223 O << " - ";
224 DispVal = -DispVal;
225 }
226 O << DispVal;
227 }
228 }
229 O << "]";
230}
231
Evan Cheng5588de92006-02-18 00:15:05 +0000232void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
233 O << "\"L" << getFunctionNumber() << "$pb\"\n";
234 O << "\"L" << getFunctionNumber() << "$pb\":";
235}
Chris Lattnerb9740462005-07-01 22:44:09 +0000236
237/// printMachineInstruction -- Print out a single X86 LLVM instruction
238/// MI in Intel syntax to the current output stream.
239///
240void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
241 ++EmittedInsts;
242
243 // Call the autogenerated instruction printer routines.
244 printInstruction(MI);
245}
246
247bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner9f6ce0e2005-07-03 17:34:39 +0000248 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerb9740462005-07-01 22:44:09 +0000249 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
250 //
251 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
252 // instruction as a reference to the register named sp, and if you try to
253 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
254 // before being looked up in the symbol table. This creates spurious
255 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
256 // mode, and decorate all register names with percent signs.
257 O << "\t.intel_syntax\n";
258 return false;
259}
260
261// Include the auto-generated portion of the assembly writer.
262#include "X86GenAsmWriter1.inc"